【发布时间】:2014-10-23 23:30:40
【问题描述】:
为什么我的网络应用在抛出异常时返回 200?即使我得到了正确的错误响应正文。
一个示例响应是:
Status Code = 200
Body: { "error": "User does not exist" }
我的代码如下:
@RequestMapping(method = RequestMethod.PUT, value = "/{userId}")
@ResponseStatus(HttpStatus.OK)
public void updateUser(@PathVariable("userId") String userId,
@RequestBody(required = false) Map<String, String> fields)
throws UserNotFoundException {
// code that leads to the exception being thrown
}
@ExceptionHandler(UserNotFoundException.class)
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorDto userNotFound() {
return new ErrorDto("User does not exist");
}
【问题讨论】:
标签: java spring exception-handling