web-services - 通过 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 上引用了几个示例,例如this、this和this,并且我看到我提供了正确的参数,但我仍然在 Postman 中收到 415 Unsupported Media Type 错误。我在这个项目中还有其他几个使用 MediaType application/json 的 Web 服务,因此项目配置应该不是问题。
有人可以说明这里有什么问题吗?
更新:添加了与使用的所有球衣和 WS 相关的依赖项以及 web.xml 文件中的重要内容相关的其他详细信息
解决方案
推荐阅读
- selenium - 如何在 Ubuntu 20.04 上将 chrome 降级到版本 87.0.4280.88?
- python - 无法重新格式化字符串
- javascript - 如何将带有数组的对象变成数组?
- python - 如何在python中的特定行从字符串的开头和结尾删除引号?
- spring-boot - Mybatis - 两个Mybatis文件可以使用同一张临时表吗?
- azure - 使用 Azure Java SDK 创建 Azure BlobContainerClient 实例是繁重的操作
- visual-studio - 我可以像在 JetBrains 的 IDE 中那样在 Visual Studio 中设置一个以上的键盘光标吗?
- r - 我在一台计算机上收到此错误“UseMethod(“train”)错误”,而在另一台计算机上此脚本有效。为什么?
- python - 参数数量不足的 lambda 函数
- python - 如何使用 kwargs 对 Django ModelForms 进行单元测试