multipart/form-data request not working using dropwizard-client and dropwizard-forms

2,048 views
Skip to first unread message

Florian

unread,
Feb 22, 2016, 12:24:24 PM2/22/16
to Swagger
Hi all,

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>

In my application class:

 public void initialize(Bootstrap<SimpleConfiguration> bootstrap) {
        // web services
        bootstrap.addBundle(jaxWsBundle);
        // support for multi-part forms
        bootstrap.addBundle(new MultiPartBundle());
 }

 public void run(SimpleConfiguration configuration, Environment environment) {
  // REST client (for external service)
  final Client client = new JerseyClientBuilder(environment).using(
    configuration.getJerseyClientConfiguration()).build(getName());
  // register JAX-RS resources
  final TestResource ds = new TestResource(client);
  environment.jersey().register(ds);  
  ...
 }

In my resources class:

@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...

Anyone any ideas on this?

Thanks and regards,
Florian

Ron Ratovsky

unread,
Feb 22, 2016, 1:23:25 PM2/22/16
to swagger-sw...@googlegroups.com
This is related to Swagger? :)

From: <swagger-sw...@googlegroups.com> on behalf of Florian <aufdemh...@web.de>
Reply-To: "swagger-sw...@googlegroups.com" <swagger-sw...@googlegroups.com>
Date: Monday, 22 February 2016 at 09:24
To: Swagger <swagger-sw...@googlegroups.com>
Subject: multipart/form-data request not working using dropwizard-client and dropwizard-forms

Hi all,

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-2216:39:21,124] org.glassfish.jersey.message.internal.WriterInterceptorExecutor:MessageBodyWriternot 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:MessageBodyWriternot found for media type=multipart/form-data, type=class org.glassfish.jersey.media.multipart.MultiPart, genericType=class org.glassfish.jersey.media.multipart.MultiPart.
...
 

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Florian

unread,
Feb 22, 2016, 3:47:38 PM2/22/16
to Swagger
Aaahh ... sorry! Of course, this is not related to swagger. Seems like I mixed up all the opened tabs in my browser and posted this to the wrong group. Sorry!

... and for anyone interested, this might solve the problem:

// REST client (for externalservice)
final Client client = new JerseyClientBuilder(environment)
         
.using(
configuration
.getJerseyClientConfiguration())
         
.withProvider(MultiPartFeature.
class)
         
.build(getName());

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages