泽西岛2多部分pojo始终为null [英] jersey 2 multipart pojo is always null

查看:107
本文介绍了泽西岛2多部分pojo始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Rest服务,使用Jersey + Jackson来上传文件以及其他一些文件信息.

I'm trying to write a rest service to upload a file along with some other file information, using Jersey + Jackson.

使用多部分功能,文件可以正确上传,并且简单字段也可以,但是应该包含其他数据的POJO始终为空.

Using multipart, the file is uploaded correctly, and simple fields are OK as well, but the POJO that's supposed to contain additional data, is always null.

简化示例

POJO:

public class Test {

 public String name;

 public Test() {}

 public String getName() {
    return name;
 }
 public void setName(String name) {
    this.name = name;
 }
}

应用程序:

@ApplicationPath("myapp")

public class JerseyApp extends ResourceConfig {

 public JerseyApp() {

  register(MultiPartFeature.class);

  register(JacksonFeature.class);

  packages("com.test.rest");

  // Enable Tracing support.
  property(ServerProperties.TRACING, "ALL");
 }
}

服务:

@Path("file")
public class FileRestService {

 @POST
 @Path("/upload1")
 @Consumes(MediaType.MULTIPART_FORM_DATA)
 public Response createFile1(@FormDataParam("doc") Test doc) {
    //doc is always null
    return Response.ok(doc.getName()).build();
 }

 @POST
 @Path("/upload2")
 @Consumes(MediaType.APPLICATION_JSON)
 public Response createFile2(Test doc) {
    //doc is created ok
    return Response.ok(doc.getName()).build();
 }

web.xml为空

pom.xml

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.22</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>          
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.22</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.22</version>
    </dependency>       
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.22</version>
    </dependency>       
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>org.fusesource.jansi</groupId>
        <artifactId>jansi</artifactId>
        <version>1.11</version>
    </dependency>
</dependencies>

数据是JSON,如果有任何区别,我正在与DHC/Postman进行测试.

Data is JSON and I'm testing with DHC/Postman, if it makes any difference.

您知道为什么使用multipart时pojo/bean为null吗?

Any idea why when using multipart, the pojo/bean is null?

推荐答案

此处查看相关问题.问题是没有为doc部分设置Content-Type.在该帖子(答案)中,我不知道如何在Postman中进行设置,而且仍然没有找到解决方案.

See related problem here. The problem is that the Content-Type is not set for the doc part. In that post (answer) I didn't know how to set it in Postman, and I still haven't found a solution.

如果您使用 cURL 之类的工具(我只是说这是有史以来REST开发的最佳工具: -),可以设置每个部分的Content-Type.如果您还不知道cURL是一个类似工具的命令,则可以用来发出HTTP(和其他协议)请求.例如,您可以执行类似

If you use a tool like cURL (which I'll just say is the best tool ever for REST development :-), you can make set the Content-Type of each part. If you don't know already cURL is a command like tool that you can use to make HTTP (and other protocol) requests. For example, you can do something like

curl -v -X POST http://localhost:8080/api/file \
        -F 'doc={"hello":"world"};type=application/json'

这会将POST请求分为多个部分,并将doc部分设置为application/json类型.

This makes a POST request as multipart and sets the doc part to be of type application/json.

您还将在此处

如果您根本无法设置各个部分的Content-Type,则另一个选项是在反序列化之前以编程方式设置类型.例如

Another options, if you simply can't set the individual parts' Content-Type, is to set the type programmatically before deserialing. For example

 @POST
 @Path("/upload1")
 @Consumes(MediaType.MULTIPART_FORM_DATA)
 public Response createFile1(@FormDataParam("doc") FormDataBodyPart part) {
    part.setMediaType(MediaType.APPLICATION_JSON_TYPE);
    Test doc = part.getValueAs(Test.class);
    return Response.ok(doc.getName()).build();
 }

这篇关于泽西岛2多部分pojo始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆