0

Hi I am running into a problem in that I am getting a 405 error for my delete request. All the other request methods seem fine. I have tagged the class with the @RestController annotation and have also tagged the delete method with the @ResponseBody annotation. I am currently running my application in a docker container and can see from the logs

WARN 1 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound : Request method 'DELETE' not supported

Here is my RestController class:

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Collection<User>> getUsers() {
        Collection<User> users = userService.findAll();

        return ResponseEntity.ok(users);
    }


    @RequestMapping(value = "{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<User> get(@PathVariable("id") long id) {
        User user = userService.findOne(id);

        if(user == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        return ResponseEntity.ok(user);
    }


    @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<User> post(@RequestBody User user) {
        User createdUser = userService.create(user);

        return ResponseEntity.ok(createdUser);
    }


    @RequestMapping(method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<User> put(@RequestParam(value = "id") long id, @RequestBody User editUser) {

        User user = userService.update(id, editUser);

        if(user == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        return ResponseEntity.ok(user);
    }


    @RequestMapping(value = "{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<?> delete(@PathVariable(value = "id") long id) {

        if(id == 0) {
            return new ResponseEntity<>("what are you doing", HttpStatus.BAD_REQUEST);
        }

        userService.delete(id);

        return ResponseEntity.ok("Successfully deleted user");
    }
}

and here is the error I am getting from when I try to send a DELETE request to the server

{"timestamp":1477839973484,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'DELETE' not supported","path":"/api/user/1"}

here is the unit test method:

@Test
public void deleteUser() throws Exception {

    String uri = "/api/user";
    long id = 2;

    MvcResult result = mvc.perform(MockMvcRequestBuilders.delete(uri, id)
            .accept(MediaType.APPLICATION_JSON)).andReturn();

    String content = result.getResponse().getContentAsString();

    int status = result.getResponse().getStatus();

    Assert.assertEquals("failure - expected HTTP status 200", 200, status);
    Assert.assertTrue("failure - expected HTTP response body to have a value", content.trim().length() == 0);

    User user = userService.findOne(1);

    Assert.assertNull(user);
}
DorkMonstuh
  • 819
  • 2
  • 19
  • 38
  • Are other methods GET & PUT working ? – Vasu Oct 30 '16 at 15:29
  • @javaguy yes they are, I have written unit tests for these which they all pass and give me a response code 200, just delete is giving me a 405 – DorkMonstuh Oct 30 '16 at 15:29
  • Check latest class file loaded if delete is added at the end – Vasu Oct 30 '16 at 15:32
  • @javaguy what do you mean by that? I am quite new to java as I thought I'd start new project to learn. – DorkMonstuh Oct 30 '16 at 15:34
  • Check when the class file is compiled to find that it is latest – Vasu Oct 30 '16 at 15:35
  • What URL is your unit test for delete using? – Stephen C Oct 30 '16 at 15:43
  • Hi @StephenC I will update my post with the unit test method for delete – DorkMonstuh Oct 30 '16 at 15:46
  • 2
    @GhostCat No need to be so condescending. I didn't understand javaguy _check latest class file loaded_ comment either until he translated that to "check the compiled class". It has nothing to do with skill level. I see how you were trying to help OP with your comment but honestly, the last two sentences were just unnecessary IMO. – walen Oct 31 '16 at 11:53

0 Answers0