I have some problems regarding the
use of dropwizard-client in conjunction with dropwizard-forms. I am
using version 0.9.2 of both. What I (try to) do is to call another REST
service from inside my resource class. Simple interactions (GET requests
with mapping from JSON to java objects) work as expected. Problems
occur when I try to POST to a resource that expects multipart/form-data
requests (some file data as octet-stream and some additional data as
JSON object). The REST service I am calling works when I use a Jersey
client outside of dropwizard. When calling it from inside my dropwizard
application, I always get this error message:
ERROR [2016-02-22 16:39:21,124] org.glassfish.jersey.message.internal.WriterInterceptorExecutor: MessageBodyWriter not found for media type=multipart/form-data, type=class org.glassfish.jersey.media.multipart.MultiPart, genericType=class org.glassfish.jersey.media.multipart.MultiPart.
javax.ws.rs.ProcessingException: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data, type=class org.glassfish.jersey.media.multipart.MultiPart, genericType=class org.glassfish.jersey.media.multipart.MultiPart.
...
This is what my application lookls like:
The
part of my pom.xml containing the dropwizard dependencies (the service I
am developing provides a SOAP as well as a REST API, this is why I also
require the jax-ws bundle):
<properties>
<dropwizard.version>0.9.2</dropwizard.version>
</properties>
...
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>com.github.roskart.dropwizard-jaxws</groupId>
<artifactId>dropwizard-jaxws</artifactId>
<version>0.10.0</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-client</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-forms</artifactId>
<version>${dropwizard.version}</version>
</dependency>
@javax.ws.rs.Path("/test")
public class TestResource {
private final static Logger logger = LoggerFactory
.getLogger(TestResource.class);
private final Client client;
public TestResource(Client client) {
this.client = client;
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String doGet() {
clientTest();
return "Hello, current time is " + LocalDateTime.now();
}
private void clientTest() {
WebTarget datastoreURI = client.target("http://localhost:8081/api");
// ...
// standard GET requests work as expected
// ...
// the file to upload
Path filepath = Paths.get("test.pdf");
FileInputStream fis = new FileInputStream(filepath.toFile());
// additional data to be sent with the uploaded file
FileMetadata fmd = new FileMetadata(); MultiPart multiPart = new MultiPart();
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("content",
filepath.toFile(), MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.bodyPart(fileDataBodyPart);
FormDataBodyPart bp = new FormDataBodyPart("", fmd,
MediaType.APPLICATION_JSON_TYPE);
multiPart.bodyPart(bp);
// here the error occurs!
Response r = targetURI.request().post(Entity.entity(multiPart,
MediaType.MULTIPART_FORM_DATA_TYPE));
}
Although
the creation of multipart/form-data requests results in a error,
producing multipart/form-data responses works. I added another method to
my resource to test it and there were no problems:
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@Timed
public Response createFile() {
...
}
It seems that the MessageBodyReader is present, but the Writer is missing...