Him*_*dar 14 rest json spring-mvc
我正在使用SpringMVC开发一个REST服务,我在类和方法级别有@RequestMapping.
此应用程序当前配置为返回在web.xml中配置的错误页面jsp.
<error-page>
<error-code>404</error-code>
<location>/resourceNotFound</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)
但是我想返回自定义JSON而不是此错误页面.
我可以通过在控制器中编写这个来处理异常并返回json以获取其他异常,但是当url根本不存在时,不知道如何以及在何处编写逻辑以返回JSON.
@ExceptionHandler(TypeMismatchException.class)
@ResponseStatus(value=HttpStatus.NOT_FOUND)
@ResponseBody
public ResponseEntity<String> handleTypeMismatchException(HttpServletRequest req, TypeMismatchException ex) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json; charset=utf-8");
Locale locale = LocaleContextHolder.getLocale();
String errorMessage = messageSource.getMessage("error.patient.bad.request", null, locale);
errorMessage += ex.getValue();
String errorURL = req.getRequestURL().toString();
ErrorInfo errorInfo = new ErrorInfo(errorURL, errorMessage);
return new ResponseEntity<String>(errorInfo.toJson(), headers, HttpStatus.BAD_REQUEST);
}
Run Code Online (Sandbox Code Playgroud)
我试过@ControllerAdvice,它适用于其他异常场景,但是当映射不可用时,
@ControllerAdvice
public class RestExceptionProcessor {
@Autowired
private MessageSource messageSource;
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(value=HttpStatus.NOT_FOUND)
@ResponseBody
public ResponseEntity<String> requestMethodNotSupported(HttpServletRequest req, HttpRequestMethodNotSupportedException ex) {
Locale locale = LocaleContextHolder.getLocale();
String errorMessage = messageSource.getMessage("error.patient.bad.id", null, locale);
String errorURL = req.getRequestURL().toString();
ErrorInfo errorInfo = new ErrorInfo(errorURL, errorMessage);
return new ResponseEntity<String>(errorInfo.toJson(), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(NoSuchRequestHandlingMethodException.class)
@ResponseStatus(value=HttpStatus.NOT_FOUND)
@ResponseBody
public ResponseEntity<String> requestHandlingMethodNotSupported(HttpServletRequest req, NoSuchRequestHandlingMethodException ex) {
Locale locale = LocaleContextHolder.getLocale();
String errorMessage = messageSource.getMessage("error.patient.bad.id", null, locale);
String errorURL = req.getRequestURL().toString();
ErrorInfo errorInfo = new ErrorInfo(errorURL, errorMessage);
return new ResponseEntity<String>(errorInfo.toJson(), HttpStatus.BAD_REQUEST);
}
}
Run Code Online (Sandbox Code Playgroud)
Him*_*dar 24
在SpringFramework中挖掘DispatcherServlet和HttpServletBean.init()后,我发现它在Spring 4中是可能的.
org.springframework.web.servlet.DispatcherServlet
/** Throw a NoHandlerFoundException if no Handler was found to process this request? **/
private boolean throwExceptionIfNoHandlerFound = false;
protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (pageNotFoundLogger.isWarnEnabled()) {
String requestUri = urlPathHelper.getRequestUri(request);
pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + requestUri +
"] in DispatcherServlet with name '" + getServletName() + "'");
}
if(throwExceptionIfNoHandlerFound) {
ServletServerHttpRequest req = new ServletServerHttpRequest(request);
throw new NoHandlerFoundException(req.getMethod().name(),
req.getServletRequest().getRequestURI(),req.getHeaders());
} else {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
}
Run Code Online (Sandbox Code Playgroud)
throwExceptionIfNoHandlerFound默认为false,我们应该在web.xml中启用它
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用此方法在使用@ControllerAdvice注释的类中捕获它.
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseStatus(value=HttpStatus.NOT_FOUND)
@ResponseBody
public ResponseEntity<String> requestHandlingNoHandlerFound(HttpServletRequest req, NoHandlerFoundException ex) {
Locale locale = LocaleContextHolder.getLocale();
String errorMessage = messageSource.getMessage("error.bad.url", null, locale);
String errorURL = req.getRequestURL().toString();
ErrorInfo errorInfo = new ErrorInfo(errorURL, errorMessage);
return new ResponseEntity<String>(errorInfo.toJson(), HttpStatus.BAD_REQUEST);
}
Run Code Online (Sandbox Code Playgroud)
这允许我返回没有映射的坏URL的JSON响应,而不是重定向到JSP页面:)
{"message":"URL does not exist","url":"http://localhost:8080/service/patientssd"}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Spring Boot,请设置以下两个属性的BOTH:
spring.resources.add-mappings=false
spring.mvc.throw-exception-if-no-handler-found=true
Run Code Online (Sandbox Code Playgroud)
现在你的@ControllerAdvice注释类可以处理"NoHandlerFoundException",如下所示.
@ControllerAdvice
@RequestMapping(produces = "application/json")
@ResponseBody
public class RestControllerAdvice {
@ExceptionHandler(NoHandlerFoundException.class)
public ResponseEntity<Map<String, Object>> unhandledPath(final NoHandlerFoundException e) {
Map<String, Object> errorInfo = new LinkedHashMap<>();
errorInfo.put("timestamp", new Date());
errorInfo.put("httpCode", HttpStatus.NOT_FOUND.value());
errorInfo.put("httpStatus", HttpStatus.NOT_FOUND.getReasonPhrase());
errorInfo.put("errorMessage", e.getMessage());
return new ResponseEntity<Map<String, Object>>(errorInfo, HttpStatus.NOT_FOUND);
}
}
Run Code Online (Sandbox Code Playgroud)
注意仅仅指定此属性是不够的:
spring.mvc.throw-exception-if-no-handler-found=true
Run Code Online (Sandbox Code Playgroud)
,默认情况下Spring将未知的URL映射到/**,所以真的永远不会"找不到处理程序".
要禁用未知的URL映射到/**,您需要
spring.resources.add-mappings=false ,
Run Code Online (Sandbox Code Playgroud)
这就是为什么两个属性共同产生所需的行为.
| 归档时间: |
|
| 查看次数: |
25722 次 |
| 最近记录: |