Search code examples
javaspringspring-bootgethttp-delete

Request method 'GET' not supported] error for method using @DeleteMapping


I'm getting a error like:

Request method 'GET' not supported for a method (deleteProductById)

using the @DeleteMapping annotation whenever I visit the URL mapped to said method (http://localhost:8083/deleteproductbyid/1). The app works when I change the annotation for the method to @GetMapping but that causes other issues.

Here's the relevant code:

package com.democrudexample.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.democrudexample.model.Product;
import com.democrudexample.services.CrudService;

@RestController
@RequestMapping("/")
public class CrudRestController {

    @Autowired
    private CrudService service;
    
    @GetMapping("/getproductlist")
    @CrossOrigin(origins = "http://localhost:4200")
    public List<Product> fetchProductList() {
        List<Product> products = new ArrayList<Product>();
        //logic to fetch list from database
        products = service.fetchProductList();
        return products;
    }
    
    @PostMapping("/addproduct")
    @CrossOrigin(origins = "http://localhost:4200")
    public Product saveProduct(@RequestBody Product product) {
        return service.saveProductToDB(product);
    }
    
    @GetMapping("/getproductbyid/{id}")
    @CrossOrigin(origins = "http://localhost:4200")
    public Product fetchProductById(@PathVariable int id) {
        return service.fetchProductById(id).get();
        
    }
    
    @DeleteMapping(value = "/deleteproductbyid/{id}")
    @CrossOrigin(origins = "http://localhost:4200")
    public String deleteProductById(@PathVariable int id) {
        return service.deleteProductById(id);
    }
}
package com.democrudexample.services;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.democrudexample.model.Product;
import com.democrudexample.repository.CrudRepo;

@Service
public class CrudService {

    @Autowired
    private CrudRepo repo;
    
    public List<Product> fetchProductList(){
        return repo.findAll();
    }
    
    public Product saveProductToDB(Product product) {
        return repo.save(product);
    }
    
    public Optional<Product> fetchProductById(int id) {
        
        return repo.findById(id);
    }
    
    public String deleteProductById(int id) {
        String result;
        try {
            repo.deleteById(id);
            result = "Product sucessfully deleted";
            System.out.println(result);
        }catch(Exception e) {
            result = "Product id is not valid";
            System.out.println(result);
        }
        
        return result;
    }
}

EDIT: I commented out the result and everything related to it in the deleteProductById method and it seems to be working just fine now. After having looked at the console, the error seems to have been some issues with parsing the text.


Solution

  • By annotating the method as @DeleteMapping, you are making this a HTTP DELETE operation. Refer this documentation for more details about different HTTP requests.

    However, when you access an URL in browser, browser always sends a GET request, whereas your Resource is expecting a DELETE request. Hence you are getting the error.

    You can use tools like Postman or you can write a small code in Javascript to send a DELETE request to the server.