你不能有两个Content-Types(从技术上讲,这就是我们在下面所做的,但它们与多部分的每个部分分开,但主要类型是多部分)。这基本上就是您对方法的期望。您期望 mutlipart 和 json 一起作为主要媒体类型。 Employee 数据需要是多部分的一部分。因此,您可以为Employee 添加@FormDataParam("emp")。
@FormDataParam("emp") Employee emp) { ...
这是我用来测试的类
@Path("/multipart")
public class MultipartResource {
@POST
@Path("/upload2")
@Consumes({MediaType.MULTIPART_FORM_DATA})
public Response uploadFileWithData(
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition cdh,
@FormDataParam("emp") Employee emp) throws Exception{
Image img = ImageIO.read(fileInputStream);
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)));
System.out.println(cdh.getName());
System.out.println(emp);
return Response.ok("Cool Tools!").build();
}
}
首先,我刚刚使用客户端 API 进行了测试,以确保它可以正常工作
@Test
public void testGetIt() throws Exception {
final Client client = ClientBuilder.newBuilder()
.register(MultiPartFeature.class)
.build();
WebTarget t = client.target(Main.BASE_URI).path("multipart").path("upload2");
FileDataBodyPart filePart = new FileDataBodyPart("file",
new File("stackoverflow.png"));
// UPDATE: just tested again, and the below code is not needed.
// It's redundant. Using the FileDataBodyPart already sets the
// Content-Disposition information
filePart.setContentDisposition(
FormDataContentDisposition.name("file")
.fileName("stackoverflow.png").build());
String empPartJson
= "{"
+ " \"id\": 1234,"
+ " \"name\": \"Peeskillet\""
+ "}";
MultiPart multipartEntity = new FormDataMultiPart()
.field("emp", empPartJson, MediaType.APPLICATION_JSON_TYPE)
.bodyPart(filePart);
Response response = t.request().post(
Entity.entity(multipartEntity, multipartEntity.getMediaType()));
System.out.println(response.getStatus());
System.out.println(response.readEntity(String.class));
response.close();
}
我刚刚创建了一个简单的 Employee 类,其中包含一个用于测试的 id 和 name 字段。这工作得很好。它显示图像,打印内容配置,并打印Employee 对象。
我对 Postman 不太熟悉,所以我把测试留到最后 :-)
它似乎也可以正常工作,您可以看到回复 "Cool Tools"。但是如果我们查看打印的Employee 数据,我们会发现它是空的。这很奇怪,因为客户端 API 可以正常工作。
如果我们查看预览窗口,就会发现问题
emp 正文部分没有 Content-Type 标头。您可以在客户端 API 中看到我明确设置它
MultiPart multipartEntity = new FormDataMultiPart()
.field("emp", empPartJson, MediaType.APPLICATION_JSON_TYPE)
.bodyPart(filePart);
所以我想这实际上只是完整答案的部分。就像我说的,我不熟悉邮递员所以我不知道如何为各个身体部位设置Content-Types。图像的image/png 是自动为我设置的图像部分(我猜它只是由文件扩展名决定的)。如果你能弄清楚这一点,那么问题应该得到解决。请,如果您知道如何执行此操作,请将其发布为答案。
请参阅下面的更新以获取解决方案
只是为了完整性......
基本配置:
依赖:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey2.version}</version>
</dependency>
客户端配置:
final Client client = ClientBuilder.newBuilder()
.register(MultiPartFeature.class)
.build();
服务器配置:
// Create JAX-RS application.
final Application application = new ResourceConfig()
.packages("org.glassfish.jersey.examples.multipart")
.register(MultiPartFeature.class);
如果您在服务器配置方面遇到问题,以下帖子之一可能会有所帮助
更新
从 Postman 客户端可以看出,一些客户端无法设置单个部分的 Content-Type,这包括浏览器,因为它是使用 FormData (js) 时的默认功能。
我们不能指望客户端绕过这个,所以我们可以做的是,在接收数据时,在反序列化之前显式设置 Content-Type。例如
@POST
@Path("upload2")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFileAndJSON(@FormDataParam("emp") FormDataBodyPart jsonPart,
@FormDataParam("file") FormDataBodyPart bodyPart) {
jsonPart.setMediaType(MediaType.APPLICATION_JSON_TYPE);
Employee emp = jsonPart.getValueAs(Employee.class);
}
获取 POJO 需要做一些额外的工作,但它比强迫客户尝试找到自己的解决方案更好。
另一种选择是使用字符串参数并使用您使用的任何 JSON 库将字符串反序列化到 POJO(如 Jackson ObjectMapper)。使用前面的选项,我们只让 Jersey 处理反序列化,它将使用与所有其他 JSON 端点相同的 JSON 库(这可能是首选)。
旁白