I'm trying to write a rest service to upload a file along with some other file information, using Jersey + Jackson.
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.
Simplified example
POJO:
public class Test {
public String name;
public Test() {}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Application:
@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");
}
}
Service:
@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 is empty
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>
Data is JSON and I'm testing with DHC/Postman, if it makes any difference.
Any idea why when using multipart, the pojo/bean is null?