我使用Spring(4.0.4)MVC构建RESTful API。我需要用一个错误的HTTP状态JSON表示返回ResponseEntity 404从404上的RESTful Spring API返回错误JSON(模糊@ExceptionHandler)
然而,当我做,结果在一个404的请求,而不是看到的错误JSON,我得到下面的输出:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /project/api/resource/100 was not found on this server.</p>
<p>Additionally, a 503 Service Temporarily Unavailable
error was encountered while trying to use an ErrorDocument to handle the request.</p>
</body></html>
从this stackoverflow post,我发现我可以防止这种错误页面由包括下列参数在web.xml中我
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
并采用全球到位错误发送与此类似的建议:
@ControllerAdvice
public class MyExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(value = HttpStatus.NOT_FOUND)
@ResponseBody
public ResponseEntity<ErrorResponse> requestHandlingNoHandlerFound(HttpServletRequest req,
NoHandlerFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse();
errorResponse.setText("error response");
return new ResponseEntity<ErrorResponse>(errorResponse, HttpStatus.BAD_REQUEST);
}
}
然而,当我添加此MyExceptionHandler类,我得到以下异常:
Caused by: java.lang.IllegalStateException: Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.servlet.NoHandlerFoundException]: {public org.springframework.http.ResponseEntity com.rest.MyExceptionHandler.requestHandlingNoHandlerFound
的问题是,它不知道是否要打电话给我定制的ExceptionHandler或定义的web.servlet。
如何解决此问题,以便能够使用Spring从API返回JSON?
不幸的是,解决方案1和2都解决了我遇到的问题。 我认为问题在于ResponseEntityExceptionHandler类为NoHandlerFoundException定义了一个不能被覆盖的最终处理程序。 有没有人有任何其他建议,我可以如何返回与Spring REST的错误JSON或如何克服模糊的@ExceptionHandler问题? – greenJavaDev
你说得对。我写了一些示例代码,并能够重现此问题。您是使用Spring Boot还是Spring MVC的常规Web应用程序? – Jigish
Spring MVC webapp – greenJavaDev