无法捕获到异常(Multipart Mime零件文件超过了最大文件大小)

时间:2018-07-11 11:56:41

标签: spring-boot

我的springboot版本是2.0.2,然后在控制器类中创建全局Exception,例如:

@ExceptionHandler(Exception.class)
@ResponseBody
public Result handleException(Exception e, HttpSession session, HttpServletRequest request) {
    LOGGER.error(e.getMessage());

但是我无法捕获到异常,看来我应该在控制器之前捕获异常,怎么办?

  

org.eclipse.jetty.http.BadMessageException:400:无法解析表单   内容         在org.eclipse.jetty.server.Request.getParameters(Request.java:390)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.Request.getParameter(Request.java:1025)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75)   〜[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]         在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]         在org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1642)   〜[jetty-servlet-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)   〜[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]         在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)   〜[spring-web-5.0.6.RELEASE.jar:5.0.6.RELEASE]         在org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1642)   〜[jetty-servlet-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)   〜[jetty-servlet-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)   〜[jetty-security-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)   〜[jetty-servlet-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.Server.handle(Server.java:531)〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:352)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.io.AbstractConnection $ ReadCallback.succeeded(AbstractConnection.java:281)   〜[jetty-io-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)   〜[jetty-io-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.io.ChannelEndPoint $ 2.run(ChannelEndPoint.java:118)   〜[jetty-io-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.runTask(EatWhatYouKill.java:333)   〜[jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:310)   〜[jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.tryProduce(EatWhatYouKill.java:168)   〜[jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:132)   〜[jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:760)   〜[jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.util.thread.QueuedThreadPool $ 2.run(QueuedThreadPool.java:678)   〜[jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]         在java.lang.Thread.run(Thread.java:748)[?:1.8.0_131]       引起原因:java.lang.IllegalStateException:多部分Mime零件文件超过了最大文件大小         在org.eclipse.jetty.util.MultiPartInputStreamParser $ MultiPart.write(MultiPartInputStreamParser.java:174)   〜[jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.util.MultiPartInputStreamParser.parse(MultiPartInputStreamParser.java:812)   〜[jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.util.MultiPartInputStreamParser.getParts(MultiPartInputStreamParser.java:493)   〜[jetty-util-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.MultiParts $ MultiPartsUtilParser。(MultiParts.java:121)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.Request.newMultiParts(Request.java:2410)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.Request.getParts(Request.java:2333)〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.Request.extractContentParameters(Request.java:479)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         在org.eclipse.jetty.server.Request.getParameters(Request.java:386)   〜[jetty-server-9.4.10.v20180503.jar:9.4.10.v20180503]         ...另外35个

1 个答案:

答案 0 :(得分:1)

一种解决方案是使用ControllerAdvice。看下面的例子:

@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler(value = {MaxUploadSizeExceededException.class})
    protected ResponseEntity<String> handle(MaxUploadSizeExceededException ex) {
        return ResponseEntity.ok(ex.getMessage());
    }
}

或者,您可以  如果您具有面向视图的控制器,则让该控制器从HandlerExceptionResolver扩展。请看下面的例子:

@Controller
public class MyController implements HandlerExceptionResolver {
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public ModelAndView someMethod(@RequestParam("file") MultipartFile file) throws Exception{
        return new ModelAndView("success");
    }

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        if (e instanceof MaxUploadSizeExceededException){
            return new ModelAndView("sizeLimitExceeded");
        }
        return new ModelAndView("otherException");
    }
}