First method:
@RequestMapping(value = "/api/version/products", method = RequestMethod.GET)
public ModelAndView getAllProducts() throws IOException {
List<Product> products = productService.findAllProducts();
ModelAndView model = new ModelAndView("main");
model.addObject("products", products);
return model;
}
Second method:
@RequestMapping(value = "/api/version/products/{id}", method = RequestMethod.DELETE)
public String deleteProduct(@PathVariable String id) throws IOException {
Product productToDelete = productService.findById(id);
if (productToDelete != null) {
productService.deleteProduct(id);
}
return "redirect:/api/version/products";
}
After DELETE request I got this message:
HTTP Status 405 - Request method 'DELETE' not supported
Third method working correctly:
@RequestMapping(value = "/api/version/products", method = RequestMethod.POST)
public String addProduct(@RequestParam String name, @RequestParam String price) throws IOException {
Product product = new Product();
product.setName(name);
product.setPrice(new BigDecimal(price));
productService.saveProduct(product);
return "redirect:/api/version/products";
}
1) Try adding a GET request handler(redirect to some other page or something) with the same URL path as your DELETE handler
@RequestMapping(value = "/api/version/products/{id}", method = RequestMethod.GET)
public String getProduct(@PathVariable String id) throws IOException {
return "redirect:/";
}
I once had a problem like this and this solved it
2) try: "api/version/products/{id}" instead of "/api/version/products/{id}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With