0

I am using SpringBoot 1.2.3.RELEASE and it is a simple SpringMVC web app using thymeleaf and jquery.

My Controller:

@Controller
@RequestMapping(value="/cart")
public class CartController
{
    @RequestMapping(value="", method=RequestMethod.GET)
    public String showCart(HttpServletRequest request, Model model)
    {
        Cart cart = getOrCreateCart(request);
        model.addAttribute("cart", cart);
        return "cart";
    }

    @RequestMapping(value="/items", method=RequestMethod.PUT)
    public String updateCartItem(@RequestBody LineItem item, HttpServletRequest request, HttpServletResponse response)
    {
        Cart cart = getOrCreateCart(request);
        cart.updateItemQuantity(item.getProduct(), item.getQuantity());
        return "redirect:/cart";
    }
}

And I am sending PUT request using jquery as follows:

$.ajax ({ 
    url: 'cart/items', 
    type: "PUT", 
    dataType: "json",
    contentType: "application/json",
    data : '{ "product" :{ "sku":"'+ sku +'"},"quantity":"'+quantity+'"}',  
    success: function(responseData, status, xhttp){ 
        alert(responseData); 
        //location.reload(); 
    }
});

When this PUT request is triggered its reaching updateCartItem() method and then with return "redirect:/cart"; it is throwing PUT http://localhost:8080/cart 405 (Method Not Allowed) error.

Why redirect view is carrying the same PUT method for redirect url?

I have seen similar here at 405 JSP error with Put Method

How can I fix this in spring way?

Community
  • 1
  • 1
K. Siva Prasad Reddy
  • 11,786
  • 12
  • 68
  • 95

1 Answers1

0

try:

$.ajax({
    url: 'cart/items',
    type: 'POST',
    data: {
        _method: 'PUT'
    },
    success: function(data) {
        ...
    }
});

add some lines in springmvc web.xml

<filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>  
    <filter-name>HiddenHttpMethodFilter</filter-name>  
    <servlet-name>springmvc</servlet-name>  
</filter-mapping> 
Leo Silence
  • 1,192
  • 11
  • 22
  • springboot already registers the HiddenHttpMethodFilter. this will only enables to send PUT, but this is already working. Problem is redirect should issue GET request instead of PUT. – K. Siva Prasad Reddy Jun 02 '15 at 02:55
  • have you try `@RequestMapping(value="", method=RequestMethod.GET)` remove `method=RequestMethod.GET` ? – Leo Silence Jun 02 '15 at 03:00
  • that method should serve GET only because other methods have different actions on the url /cart – K. Siva Prasad Reddy Jun 02 '15 at 03:04
  • 1
    use ajax you can do ` success: function(data) { location.href = '/cart' }` – Leo Silence Jun 02 '15 at 03:09
  • That's a work around which will work. I will explore how it behaves with plain form submit and using jquery $.ajax(). I am thinking if it is ajax request then its preserving the RequestMethod for subsequent request. – K. Siva Prasad Reddy Jun 02 '15 at 04:12