【发布时间】:2017-06-02 04:26:40
【问题描述】:
我使用下面的代码来处理类中所有RuntimeException类型的异常@ControllerAdvice注解
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<JSONObject> RuntimeExceptionHandler(RuntimeException e) throws JSONException {
JSONObject response = new JSONObject();
response.put("message", e.getMessage());
return new ResponseEntity<JSONObject>(response, HttpStatus.BAD_REQUEST);
}
如果是ValidationException,它会向客户端返回以下响应:
{
"timestamp": 1496377230943,
"status": 500,
"error": "Internal Server Error",
"exception": "javax.validation.ValidationException",
"message": "Name does not meet expectations",
"path": "/signup"
}
这不是我所期望的。状态码不是BAD_REQUEST 且json 与response 不同。
如果我将 JSONObject 更改为 String 并传入字符串消息而不是 json 对象,它工作正常。我还在return 语句之前放置了一个断点,response 看起来不错。
注意:还有一个帖子here:
- 没有接受的答案。
- 我没有用
@ResponseBody注释方法。 - 没有使用
JSONObject
【问题讨论】:
-
你有什么问题?
-
嗯,反应不是应该的。