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

使用Jersey上传XML文件时遭遇HTTP Status 415不支持媒体类型错误

Troubleshooting "HTTP Status 415 - Unsupported Media Type" in Jersey XML File Upload

Hey there, let's work through that 415 error you're facing when uploading XML files with Jersey. This error usually means either your request isn't sending the right media type, or Jersey isn't set up to handle the type you're sending. Here are the key checks to fix this:

1. Verify Your Request's Content-Type Header

First, double-check what Content-Type your browser is sending:

  • If you're directly POSTing raw XML content (not via a file upload form), the header must be application/xml (or text/xml for unvalidated XML). Browsers might default to other types if you don't explicitly set this.
  • If you're using an HTML form with <input type="file"> to upload the XML file, the request will use multipart/form-data—this requires extra Jersey configuration (see point 3 below).

2. Fix Jersey XML Provider Dependencies

You're using Jersey 1.x (v1.4.0), which relies on JAXB for XML processing. Looking at your jar list, you're missing critical JAXB dependencies that Jersey needs to parse XML. Add these to your project:

  • jaxb-api (JAXB API)
  • jaxb-impl (JAXB implementation)
  • activation.jar (JavaBeans Activation Framework, required for JAXB)

These are essential for Jersey to recognize and process application/xml media types.

3. Configure Multipart Handling (If Using File Upload Forms)

If you're uploading the XML file via a form, you need to properly set up Jersey's multipart support:

  • Make sure your jersey-multipart-1.4 jar is correctly included (you already have this, good start).
  • Register the MultipartProvider in your Jersey configuration:
    • If using an Application subclass:
      @Override
      public Set<Class<?>> getClasses() {
          Set<Class<?>> classes = new HashSet<>();
          classes.add(YourUploadResource.class); // Replace with your resource class
          classes.add(MultiPartProvider.class); // Register the multipart handler
          return classes;
      }
      
    • If using web.xml:
      <servlet>
          <servlet-name>Jersey Servlet</servlet-name>
          <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
          <init-param>
              <param-name>com.sun.jersey.config.property.packages</param-name>
              <param-value>com.your.package;com.sun.jersey.multipart</param-value>
          </init-param>
      </servlet>
      
  • Ensure your resource method uses the right annotations for multipart:
    @POST
    @Path("/upload-xml")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadXmlFile(
        @FormDataParam("xml-file") InputStream fileStream,
        @FormDataParam("xml-file") FormDataContentDisposition fileDetails
    ) {
        // Process your XML file here
        return Response.ok("XML file uploaded successfully").build();
    }
    

4. Resolve Version Conflicts Between Jackson and Jersey

You're mixing Jersey 1.4.0 with Jackson 2.3.2, which can cause compatibility issues. Jersey 1.x is designed to work with Jackson 1.x (not 2.x) out of the box. To fix this:

  • Either downgrade your Jackson dependencies to a 1.x version (like 1.9.13), which includes jackson-jaxrs for Jersey integration.
  • Or if you want to keep Jackson 2.x, you'll need a compatibility bridge (though this is more complex—sticking to matching versions is simpler for a beginner).

5. Check Your Resource Method's @Consumes Annotation

Make sure the @Consumes annotation on your resource method matches the Content-Type of your request:

  • For raw XML POSTs: @Consumes(MediaType.APPLICATION_XML)
  • For form-based file uploads: @Consumes(MediaType.MULTIPART_FORM_DATA)

If the annotation doesn't include the media type your request is sending, Jersey will reject it with a 415 error.

Start with these checks—most likely the issue is either a missing dependency, incorrect Content-Type, or missing multipart configuration. Good luck!

内容的提问来源于stack exchange,提问作者Pankaj Dagar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:22:57