方法不允许DELETE

时间:2016-10-30 15:26:38

标签: java spring rest spring-boot

您好我遇到的问题是我的删除请求收到405错误。所有其他请求方法似乎都很好。我使用@RestController注释标记了类,并使用@ResponseBody注释标记了delete方法。我目前正在Docker容器中运行我的应用程序,可以从日志中看到

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

这是我的RestController类:

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

以下是我尝试向服务器发送DELETE请求时收到的错误

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

这是单元测试方法:

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

0 个答案:

没有答案