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