You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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请求示例

Postman请求截图

内容的提问来源于stack exchange,提问作者Sundara Balaji J K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:16:33