Tsao
Tsao

Reputation: 139

Java DeleteMapping Method Not Allowed

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,

Upvotes: 2

Views: 2479

Answers (3)

In my case this error happens when I write the code of delete method but forget to rerun the complier and java cannot find the delete method

Upvotes: 0

Dulaj Kulathunga
Dulaj Kulathunga

Reputation: 1250

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

Upvotes: 1

Uday Kumar
Uday Kumar

Reputation: 146

The reason for this error is sending the request with a non-supported method. 405 Status Code indicates that the server doesn't support the method or verb sent in the request. Could you also provide the API call details like HTTP method and relative path ? recheck your API details, make sure you are using correct HTTP method.

Upvotes: 0

Related Questions