我正在使用Spring (4.0.4) MVC来构建一个RESTful应用程序接口。我需要返回一个ResponseEntity,其中包含一个错误的JSON表示,http状态为404。
但是,当我发出一个产生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中定义的call。
如何解决此问题,以便能够使用Spring从API返回JSON?
发布于 2015-06-10 09:44:12
这是我认为正在发生的事情:
您的ExceptionHandler类扩展了ResponseEntityExceptionHandler,它有一个handleNoHandlerFoundException方法(请参阅source code中this文件末尾的部分),您还可以使用@ExceptionHandler注释方法来处理相同的异常。
@Override
ResponseEntity handleNoHandlerFoundException(NoHandlerFoundException ex,
HttpHeaders headers, HttpStatus status, WebRequest request)我认为在你的代码中同时包含这两个是导致这个问题的原因。
因此,要么:
ExceptionHandler覆盖该方法(且不使用@ExceptionHandler注释方法)
我认为这两个选项中的任何一个都应该有效。
发布于 2016-01-05 13:13:56
尝尝这个。
package x.y.z;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(value=HttpStatus.NOT_FOUND)
@ResponseBody
public ResponseEntity<String> handle404() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
String json = "{\"error\":\"Resource not found.\"}";
return new ResponseEntity<String>(json, headers, HttpStatus.NOT_FOUND);
}
}https://stackoverflow.com/questions/30743230
复制相似问题