TomEE下Jersey2实现Multipart文件上传遇415响应问题求助
Oh man, I’ve been exactly where you are—struggling with this 415 error when trying to do multipart file uploads on TomEE with Jersey 2. Let’s break down the fixes step by step:
1. You Must Register MultiPartFeature
This is the #1 mistake most people make. Jersey doesn’t enable multipart handling out of the box—you have to explicitly register the feature that handles multipart/form-data requests.
Option 1: Register via ResourceConfig
If you’re using a ResourceConfig subclass for your Jersey setup, add the feature there:
public class MyJerseyAppConfig extends ResourceConfig { public MyJerseyAppConfig() { // Scan for your resource classes packages("com.your.package.where.resources.live"); // Critical line: enable multipart processing register(MultiPartFeature.class); } }
Option 2: Register via web.xml
If you’re configuring Jersey through web.xml, add an init-param to register the feature:
<servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.your.package.where.resources.live</param-value> </init-param> <!-- Add this to enable multipart --> <init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
2. Fix TomEE-Jersey Dependency Conflicts
TomEE comes with Apache CXF as its default JAX-RS implementation. If you’re pulling in Jersey dependencies, you’ll almost certainly hit classloading conflicts. Here’s how to fix that:
Maven Dependency Tweaks
First, use the Jersey BOM to lock in consistent versions for all Jersey artifacts:
<dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey</groupId> <artifactId>jersey-bom</artifactId> <version>2.41</version> <!-- Use your actual Jersey version --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> </dependency> <!-- Add this if you need JSON support for your extra data --> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> </dependency> </dependencies>
Disable CXF JAX-RS in TomEE
If you’re using TomEE Plus, add this line to conf/system.properties to prevent CXF from taking over JAX-RS processing:
openejb.cxf.jaxrs.skip=true
3. Double-Check Your Resource Method
Your method looks mostly right, but here’s how to handle that extra JSON data cleanly. You can either accept it as a string and deserialize manually, or let Jersey map it to a POJO if you have a JSON provider set up:
@POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response upload( @FormDataParam("file") InputStream fileStream, @FormDataParam("file") FormDataContentDisposition fileDetails, @FormDataParam("meta") String metaJson) { // Extra JSON as string // Or use a POJO if you have Jackson/Gson set up: // @FormDataParam("meta") UploadMetadata metadata // Process your file and metadata here return Response.ok("Upload completed successfully").build(); }
4. Verify Your Request’s Content-Type
Make sure your client is sending the correct header. It should look like this (the boundary value will vary):
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryXYZ123
If you’re using Postman/cURL, don’t manually type this—let the tool set it when you select form-data as the request type.
Final Checks
- Restart TomEE to apply all config changes
- Check TomEE’s logs for classloading errors (like
ClassNotFoundExceptionorNoClassDefFoundError)—these are dead giveaways for dependency conflicts
内容的提问来源于stack exchange,提问作者Valentin V.

