Spring Boot+Jersey配置为Filter时,MULTIPART接口报400错误
Spring Boot + Jersey Filter模式下Multipart接口返回400错误的问题
我最近在用Spring Boot v1.5.10搭配Jersey v2.25.1开发项目时,碰到了一个头疼的矛盾问题:
- 当把Jersey配置为Filter模式时,能正常访问静态文件夹里的文件,但接收
MULTIPART_FORM_DATA的上传接口会返回HTTP 400 Bad Request错误; - 如果移除Jersey的Filter配置(默认以Servlet模式运行),上传接口能正常工作,却又访问不了静态资源了。
关键配置
设置Jersey为Filter模式时,使用的配置属性:
spring.jersey.type=filter
上传接口代码
对应的控制器接口代码如下:
@POST @Path("/save") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public ResponseBean save( @FormDataParam("fileToUpload") InputStream file, @FormDataParam("fileToUpload") FormDataContentDisposition fileDisposition, @FormDataParam("fromData") FormDataDto data) { // 业务逻辑处理代码 }
两种模式的测试结果
Filter模式(spring.jersey.type=filter)
- 根路径
http://localhost:8080/可正常访问 - 静态文件
http://localhost:8080/hello.html可正常访问 - 上传接口
http://localhost:8080/save调用失败(Postman测试返回400)
Servlet模式(移除spring.jersey.type配置)
- 根路径
http://localhost:8080/可正常访问 - 静态文件
http://localhost:8080/hello.html无法访问 - 上传接口
http://localhost:8080/save可正常工作
Postman请求示例

内容的提问来源于stack exchange,提问作者Sundara Balaji J K
相关产品推荐
相关产品推荐

