Java DeleteMapping Method Not Allowed

Tsao :

I'm trying to create an api to delete a certain ID from the storage; Here's my code.

API Controller:

@DeleteMapping("{cId}")
@ResponseStatus(HttpStatus.OK)
public String delete(@PathVariable String cId) {
    compareService.delete(cId);

    return "redirect:/compare";
}

Service:

public void delete(String cId) {
    compareLogic.delete(cId);
}

Logic:

 public void delete(String cId){
        System.out.println("A: " + sessionModel.getCIds());
        List<String> update = sessionModel.getCIds();
        update.remove(new String(cId));
        System.out.println("B: " + sessionModel.getCIds());
      }

However when I execute the api it shows

{
success: false,
warning: false,
error: "405",
error_description: "Method Not Allowed"
}

Are there any possible reasons by just looking at the code? Many thanks,

Dulaj Kulathunga :

Just I have tired with simple code snippet , Could you please try to understand and (Try to follow my suggestion as well )

When you hit from browser side (From Address Bar), it won't work for POST/PUT/DELETE calls , it is actually working from browser, if you try to typing in address bar then it is a GET request then it will not supported to the other format

Just I have added two screenshot I have tired with Browser and PostMan

First I have tired with POSTMAN (it is working perfectly)

enter image description here

Second I have tired with Browser (It will throw not supported exception )

enter image description here

I have tired with small code snippet just copy from your code and remove element from list

@DeleteMapping("{cId}")
    public String delete(@PathVariable String cId) {
         List<String> arr=new ArrayList<String>(3);
         arr.add("A");
         arr.add("B");
         arr.add("C");
         arr.remove(cId);
         for (String string : arr) {
            System.out.println(string);
        }

        return "redirect:/compare";
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=358695&siteId=1