首页 > 解决方案 > 通过 Postman 将文件上传到 Webservice 时出现 415 Unsupported Media Type 错误

问题描述

我使用 Jersey Restful API 创建了一个 Web 服务,我有以下内容:

@POST
@Path("/process/")
@Consumes({MediaType.MULTIPART_FORM_DATA})
@Produces({MediaType.APPLICATION_JSON})
public Response process(@FormDataParam("upload") InputStream is, @FormDataParam("upload") FormDataContentDisposition formData);

我使用了以下依赖项:

   <dependency>
       <groupId>javax.ws.rs</groupId>
       <artifactId>javax.ws.rs-api</artifactId>
       <version>2.1-m01</version>
   </dependency>

    <!-- https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-multipart -->
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.8</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.8</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.ws.rs/jsr311-api -->
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3</version>
    </dependency>

Web.xml 中的配置:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/ws-context.xml</param-value>
    </context-param>

在 ws-context.xml 中,我有这部分:

<bean id="restManagerService" class="com.rs.service.impl.RestManagerServiceImpl">
        <property name="restRequestService" ref="restRequestService" />
    </bean>

    <bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="100000" />
    </bean>

    <jaxrs:server id="userManagerREST" address="/rest/v1">
        <jaxrs:serviceBeans>
            <ref bean="restManagerService" />
        </jaxrs:serviceBeans>

        <jaxrs:providers>
            <ref bean='jsonProvider' />
            <ref bean='multipartResolver' />
            <bean class="com.rs.exception.ExceptionHandler" />
        </jaxrs:providers>
    </jaxrs:server>

现在为了测试这一点,我正在使用 Postman 应用程序发送一个 Post 请求。以下是代码窗口中的内容:

Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryxxxxxxxxx

------WebKitFormBoundaryxxxxxxxxx
Content-Disposition: form-data; name="upload"; filename="test.json"    

我已经在 google 上引用了几个示例,例如thisthisthis,并且我看到我提供了正确的参数,但我仍然在 Postman 中收到 415 Unsupported Media Type 错误。我在这个项目中还有其他几个使用 MediaType application/json 的 Web 服务,因此项目配置应该不是问题。

有人可以说明这里有什么问题吗?

更新:添加了与使用的所有球衣和 WS 相关的依赖项以及 web.xml 文件中的重要内容相关的其他详细信息

标签: web-servicesjerseypostman

解决方案


这可能是 Postman 中的问题,而不是您的代码。我也有这个问题。

尝试删除Content-type标题,并确保form-databody选项卡中选择:

在此处输入图像描述

在此处输入图像描述


推荐阅读