Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect after DELETE

Tags:

spring-mvc

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";
}
like image 876
Nikita Avatar asked Sep 08 '25 11:09

Nikita


1 Answers

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}"

like image 144
Vitolds Avatar answered Sep 10 '25 08:09

Vitolds