我正在尝试在Spring Rest服务中处理Validation异常,如下面给出的代码:
@Produces("application/json")
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public @ResponseBody ResponseEntity<ValidationErrorDTO> processValidationError(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
List<FieldError> fieldErrors = result.getFieldErrors();
ResponseEntity<ValidationErrorDTO> re = new ResponseEntity<>(processFieldErrors(fieldErrors), HttpStatus.BAD_REQUEST);
return re;
}
但是,当执行此代码时,它会返回附加到JSON响应的堆栈跟踪。我想摆脱这个堆栈跟踪,只返回JSON错误响应。这可以用Spring 3.1吗?
{"fieldErrors":
[{"field":"type","message":"Pattern.shippingAddress.type"}]}<pre>javax.servlet.ServletException: 400 Bad Request null
at com.ebay.raptor.kernel.dispatcher.HttpDispatchCommand.execute(HttpDispatchCommand.java:142)
at com.ebay.ebox.pres.cmd.preshandler.CommandDispatchHandler.handleRequest(CommandDispatchHandler.java:59)
at com.ebay.ebox.pres.cmd.preshandler.CommandDispatchHandler.handleRequest(CommandDispatchHandler.java:13)
at com.ebay.ebox.pres.cmd.preshandler.CommandHandlerFactory$CalHandler.handleRequest(CommandHandlerFactory.java:114)
at com.ebay.ebox.pres.cmd.preshandler.CommandHandlerFactory$CalHandler.handleRequest(CommandHandlerFactory.java:75)
at com.ebay.kernel.pipeline.RequestPipeline.invoke(RequestPipeline.java:18)
at com.ebay.kernel.pipeline.RequestPipeline.invoke(RequestPipeline.java:12)
at com.ebay.kernel.pipeline.BasePipeline.callHandler(BasePipeline.java:75)
at com.ebay.kernel.pipeline.BasePipeline.execute(BasePipeline.java:53)
at com.ebay.ebox.pres.stage.BaseCommandRequestStageImpl.doWork(BaseCommandRequestStageImpl.java:64)
at com.ebay.kernel.stage.StageDriver.execute(StageDriver.java:55)
at com.ebay.ebox.pres.cmd.WebCommandImpl.execute(WebCommandImpl.java:30)
at com.ebay.raptor.web.RaptorFrontController.process(RaptorFrontController.java:338)
at com.ebay.raptor.web.RaptorFrontController.doPost(RaptorFrontController.java:554)
at com.ebay.raptor.web.RaptorGenericController.service(RaptorGenericController.java:64)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:379)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at com.ebay.raptor.kernel.filter.RaptorDispatchFilter.doFilter(RaptorDispatchFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:195)
at com.thetransactioncompany.cors.CORSFilter.doFilter(CORSFilter.java:266)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
</pre>
答案 0 :(得分:2)
你可以这样做:
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ModelAndView methodArgumentError(HttpServletRequest req, Exception exception) {
return new JsonError(exception.getMessage()).asModelAndView();
}
答案 1 :(得分:1)
@Order(Ordered.HIGHEST_PRECEDENCE)
在您的方法上使用@Order(Ordered.HIGHEST_PRECEDENCE) 它将起作用
答案 2 :(得分:0)
同时使用@ResponseBody
和ResponseEntity
并不常见。我认为你应该清楚地决定你想做什么。 - 然后你应该检查你的内部框架类com.ebay.raptor.kernel.dispatcher.HttpDispatchCommand
,因为这是抛出异常的那个。
答案 3 :(得分:0)
您只需添加应用程序属性server.error.include-stacktrace=never
即可响应,该响应将堆栈跟踪包括在400个响应中。
答案 4 :(得分:0)
如果错误仅返回给用户而不用于调试,则可以在 MethodArgumentNotValidException 中将堆栈跟踪设置为null。
或者,您也可以使用 @JsonIgnore 批注覆盖 getStackTrace()
@JsonIgnore
@Override
public StackTraceElement[] getStackTrace() {
return super.getStackTrace();
}
编辑:我尝试了所有其他解决方案,但没有一个对我有用。
答案 5 :(得分:-1)
您可以将Aspectj用于这些目的。示例http://www.mkyong.com/spring3/spring-aop-aspectj-annotation-example/
你应该写一个方面,这将抓住你的例外。
示例:
@Aspect
public class AroundExample {
@Around
pointcut = "execution(* your.package.Class.processValidationError(..))",
throwing= "error")
public Object doSomeStuff(ProceedingJoinPoint pjp, Throwable error) throws Throwable {
Object[] args = joinPoint.getArgs(); // change the args if you want to
Object retVal = pjp.proceed(args); // run the actual method (or don't)
return retVal; // return the return value (or something else)
}
}