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?