后,我写了下面的类:春天开机默认的异常处理
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(value = Exception.class)
@ResponseBody
public ResponseEntity<Object> exceptionHandler(Exception e) {
HashMap<String, Object> msg = new HashMap<>(2);
msg.put("error", HttpStatus.PRECONDITION_FAILED.value());
msg.put("message", "Something went wrong");
return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST);
}
}
的意图是在JSON响应发送msg
,而不是放弃Spring异常是什么原因引发的。
但是,这门课并不适用。
当我打,说,和无效的端点我的服务器API,我得到默认响应有效载荷:
{
"timestamp": 1449238700342,
"status": 405,
"error": "Method Not Allowed",
"exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'POST' not supported",
"path": "/bad_enpoint"
}
我缺少什么?
谢谢。
你能详细说明“没有工作”吗?它不会被叫?它确实,但失败了? –
我用更多的信息更新了这个问题。它不会被调用,我仍然是Spring默认的JSON,并且Spring的Expect被“暴露” – sargas