1

Which parameters would be needed to handle Multipart Retrofit String and RequestBody of an image (file) Post request and which annotations?

Jax-Rs:

@POST
@Path("/user/image")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response addUserImage(?, ?) {}

Retrofit 2.0:

@Multipart
@POST("user/image")
Call<User> addUserImage(@Part("user") String userId, @Part("image") RequestBody image);
user2803086
  • 153
  • 1
  • 3
  • 10

1 Answers1

1

For a file you can use File, InputStream, or byte[]. For plain text, just use a String. You will also need to annotate the parameter with @FormDataParam("<value>"), with the value being the name of the part.

@POST
@Path("/user/image")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response addUserImage(@FormDataParam("image") InputStream image,
                             @FormDataParam("image") FormDataContentDisposition imageDetail,
                             @FormDataParam("user") String user) {   
}

You will need to make sure you have the multipart support dependency. Not sure which Jersey version you are using, but here is for both

Jersey 2.x

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>${jersey2.version}</version>
</dependency>

Jersey 1.x

<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>${jersey1.version}</version>
</dependency>

With Jersey 1.x, it should work out the box without any further configuration. For 2.x, you will need to register the MultiPartFeature. For help with that, please see this post

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720