後,我寫了下面的類:春天開機默認的異常處理
@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