我正在使用Spring 4 MVC创建REST API。 我希望,当请求无效端点时,我可以提供默认的JSON错误对象。 我已经阅读了@ControllerAdvice和@ExceptionHandler,我试图正确使用它:
package com.yopyop.wackend.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.context.request.WebRequest;
import org.springframework.web.servlet.NoHandlerFoundException;
import com.yopyop.wackend.controller.ErrorResponse;
import com.yopyop.wackend.service.NotFoundException;
@ControllerAdvice
public class ExceptionControllerAdvice {
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> exceptionHandler(Exception ex) {
ErrorResponse error = new ErrorResponse();
error.setErrorCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
if (ex.getClass()==NotFoundException.class) {
error.setMessage("ExceptionControllerAdvice() Not found");
error.setErrorCode(HttpStatus.NOT_FOUND.value());
return new ResponseEntity<ErrorResponse>(error, HttpStatus.NOT_FOUND);
}
else {
error.setMessage("ExceptionControllerAdvice() " + ex.getClass() + "(" + ex.getMessage() + ")");
error.setErrorCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
}
return new ResponseEntity<ErrorResponse>(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler({NoHandlerFoundException.class})
ResponseEntity<ErrorResponse> handleNoHandlerFoundException(NoHandlerFoundException ex,
WebRequest request) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
ErrorResponse error = new ErrorResponse();
error.setErrorCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
error.setMessage("ExceptionControllerAdvice() " + ex.getClass() + "(" + ex.getMessage() + ")");
return new ResponseEntity<ErrorResponse>(error, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
我的ErrorResponse类如下:
package com.yopyop.wackend.controller;
public class ErrorResponse {
private int errorCode;
private String message;
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
我修改了web.xml,以便抛出NoHandlerFound异常:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Wackend WebApp</display-name>
<servlet>
<servlet-name>wackend</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>
</servlet>
<servlet-mapping>
<servlet-name>wackend</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
尽管如此,当收到对未映射端点的请求时,我得到:
> 22:47:28.545 [http-nio-8080-exec-15] DEBUG
> o.s.web.servlet.DispatcherServlet - DispatcherServlet with name
> 'wackend' processing GET request for [/wackend/swagger-ui.html]
> 22:47:28.548 [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method
> for path /swagger-ui.html 22:47:28.552 [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method
> [public void
> com.yopyop.wackend.controller.DefaultController.unmappedRequest(javax.servlet.http.HttpServletRequest)
> throws org.springframework.web.servlet.NoHandlerFoundException]
> 22:47:28.552 [http-nio-8080-exec-15] DEBUG
> o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of
> singleton bean 'defaultController' 22:47:28.553
> [http-nio-8080-exec-15] DEBUG o.s.web.servlet.DispatcherServlet -
> Last-Modified value for [/wackend/swagger-ui.html] is: -1 22:47:28.566
> [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception
> from handler [public void
> com.yopyop.wackend.controller.DefaultController.unmappedRequest(javax.servlet.http.HttpServletRequest)
> throws org.springframework.web.servlet.NoHandlerFoundException]:
> org.springframework.web.servlet.NoHandlerFoundException: No handler
> found for GET /wackend/swagger-ui.html, headers={} 22:47:28.567
> [http-nio-8080-exec-15] DEBUG o.s.b.f.s.DefaultListableBeanFactory -
> Returning cached instance of singleton bean
> 'exceptionControllerAdvice' 22:47:28.567 [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Invoking
> @ExceptionHandler method:
> org.springframework.http.ResponseEntity<com.yopyop.wackend.controller.ErrorResponse>
> com.yopyop.wackend.controller.ExceptionControllerAdvice.handleNoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException,org.springframework.web.context.request.WebRequest)
> 22:47:28.603 [http-nio-8080-exec-15] ERROR
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Failed to invoke
> @ExceptionHandler method:
> org.springframework.http.ResponseEntity<com.yopyop.wackend.controller.ErrorResponse>
> com.yopyop.wackend.controller.ExceptionControllerAdvice.handleNoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException,org.springframework.web.context.request.WebRequest)
> org.springframework.web.HttpMediaTypeNotAcceptableException: Could not
> find acceptable representation at
> org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:134)
> ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] at
> org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:146)
> ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] at
> org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71)
> ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE] [...] at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
> [na:1.8.0_101] at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
> [na:1.8.0_101] at
> org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
> [tomcat-util.jar:8.0.36] at java.lang.Thread.run(Thread.java:745)
> [na:1.8.0_101] 22:47:28.603 [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception
> from handler [public void
> com.yopyop.wackend.controller.DefaultController.unmappedRequest(javax.servlet.http.HttpServletRequest)
> throws org.springframework.web.servlet.NoHandlerFoundException]:
> org.springframework.web.servlet.NoHandlerFoundException: No handler
> found for GET /wackend/swagger-ui.html, headers={} 22:47:28.603
> [http-nio-8080-exec-15] DEBUG o.s.b.f.s.DefaultListableBeanFactory -
> Returning cached instance of singleton bean
> 'exceptionControllerAdvice' 22:47:28.603 [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Invoking
> @ExceptionHandler method:
> org.springframework.http.ResponseEntity<com.yopyop.wackend.controller.ErrorResponse>
> com.yopyop.wackend.controller.ExceptionControllerAdvice.handleNoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException,org.springframework.web.context.request.WebRequest)
> 22:47:28.604 [http-nio-8080-exec-15] ERROR
> o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Failed to invoke
> @ExceptionHandler method:
> org.springframework.http.ResponseEntity<com.yopyop.wackend.controller.ErrorResponse>
> com.yopyop.wackend.controller.ExceptionControllerAdvice.handleNoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException,org.springframework.web.context.request.WebRequest)
> org.springframework.web.HttpMediaTypeNotAcceptableException: Could not
> find acceptable representation at
> org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:134)
> ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] at
> org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:146)
> ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] at
> org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71)
> ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE] at
> org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126)
> ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE] [...] at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
> [na:1.8.0_101] at
> org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
> [tomcat-util.jar:8.0.36] at java.lang.Thread.run(Thread.java:745)
> [na:1.8.0_101] 22:47:28.604 [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from
> handler [public void
> com.yopyop.wackend.controller.DefaultController.unmappedRequest(javax.servlet.http.HttpServletRequest)
> throws org.springframework.web.servlet.NoHandlerFoundException]:
> org.springframework.web.servlet.NoHandlerFoundException: No handler
> found for GET /wackend/swagger-ui.html, headers={} 22:47:28.604
> [http-nio-8080-exec-15] DEBUG
> o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from
> handler [public void
> com.yopyop.wackend.controller.DefaultController.unmappedRequest(javax.servlet.http.HttpServletRequest)
> throws org.springframework.web.servlet.NoHandlerFoundException]:
> org.springframework.web.servlet.NoHandlerFoundException: No handler
> found for GET /wackend/swagger-ui.html, headers={} 22:47:28.604
> [http-nio-8080-exec-15] DEBUG o.s.web.servlet.DispatcherServlet - Null
> ModelAndView returned to DispatcherServlet with name 'wackend':
> assuming HandlerAdapter completed request handling 22:47:28.604
> [http-nio-8080-exec-15] DEBUG o.s.web.servlet.DispatcherServlet -
> Successfully completed request
然后返回标准的Tomcat 404页面。 请注意:
返回新的ResponseEntity(错误, HttpStatus.INTERNAL_SERVER_ERROR);
将返回一个空主体的HTTP 500(优于Tomcat 默认页面)
看起来我的ErrorResponse类不能完成JSON映射。
此外,我使用默认控制器,但我认为它是无用的,因为我使用@ControllerAdvice。我将其粘贴在此处以供参考:
package com.yopyop.wackend.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.NoHandlerFoundException;
/**
* Default controller that exists to return a proper REST response for unmapped requests.
*/
@Controller
public class DefaultController {
@RequestMapping("/**")
public void unmappedRequest(HttpServletRequest request) throws NoHandlerFoundException {
String uri = request.getRequestURI();
String method = request.getMethod();
HttpHeaders h = new HttpHeaders();
throw new NoHandlerFoundException(method, uri, h);
}
}
我的REST API也使用了ResponseEntity&lt;&gt;返回并成功返回正确的JSON有效负载。
答案 0 :(得分:0)
我不知道答案到底是什么,但问题可能在于JSON配置。起初你的@Controller不是@RestController,你没有明确指定生成的内容。我希望你在你的要求中这样做,所以春天知道该怎么做。我认为来自@Controller的mime类型是继承的并用于转换。
您可以尝试在http消息转换器中添加断点。在每个转换器中都有一个mathod supports
尝试在此处捕获您的代码。检查哪个转换器返回true。如果多个条件通过,则Spring使用第一个。通过堆栈跟踪,您可能还会发现哪些转换器可用于您的请求以及需要哪种转换器。