【发布时间】:2017-12-22 06:54:04
【问题描述】:
我们有一个带有嵌入式码头的 Spring Boot 服务器,该码头公开了一个休息接口。
我们的 RestController 服务使用 "multipart/form-data"(上传文件),我们使用javax.validation(休眠)来验证请求的参数以在何时返回 BAD_REQUEST参数错误。
这里的问题是,当不给参数时,jetty和spring的过滤器会抛出异常,并且会返回一个500 INTERNAL ERROR给客户端。
在这种情况下,我们希望返回 400 BAD REQUEST,但我们找不到合适的方法,因为在尝试控制器或参数验证之前请求被拒绝(我们使 @NotNull @RequestParam (“文件”))。所以不会调用controllerAdvice
当类型为“multipart/form-data”且没有属性(multipart without any part)时,Jetty 和 Spring 过滤器拒绝 HTTP 请求
你对这个案例有什么建议?
这是堆栈跟踪
WARN 2017/07/17 18:15:52.849 CEST <Thread[qtp1020520290-18]> EXCEPTION
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: Missing content for multipart request
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982) ~[spring-webmvc-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) ~[javax.servlet-api-3.1.0.jar!/:3.1.0]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) ~[javax.servlet-api-3.1.0.jar!/:3.1.0]
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:845) ~[jetty-servlet-9.3.11.v20160721.jar!/:9.3.11.v20160721]
.........................
Caused by: org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: Missing content for multipart request
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:111) ~[spring-web-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:85) ~[spring-web-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:76) ~[spring-web-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1099) ~[spring-webmvc-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:932) ~[spring-webmvc-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.2.RELEASE.jar!/:4.3.2.RELEASE]
... 42 common frames omitted
【问题讨论】:
-
你的
web.xml?中有multipart-*标签 -
当我给一个文件时它工作得很好;该文件确实已上传,因此配置正确。我使用没有 web.xml 的 spring boot
标签: hibernate rest spring-mvc spring-boot embedded-jetty