0
votes

I have a RESTEasy Client (3.6.2.Final) implemented with a proxy interface where I want to call a WS to upload a file. The call works fine if I define a fix Content-Type, but fails if I want to set one by myself.

Interface:

@POST
@Path("api/files/upload/")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes()
FileUploadResponse uploadFile(byte[] file);

When instanciating the proxy, I provide a Basic Authentication:

ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://..."));
target.register(new BasicAuthentication("username", "password"));
ApiInterface api = target.proxy(ApiInterface.class);

Like this the call works, but the Content-Type in the header is the generic * / *

If I add a HeaderParam like this, the call fails with HTTP 400 (Bad Request):

@POST
@Path("api/files/upload/")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes()
FileUploadResponse uploadFile(byte[] file, @HeaderParam("Content-Type") String contentType);

I've checked the HTTP call and Content-Type appears twice there: once as * / * and another as "application/pdf" (the one I provide when calling uploadFile).

I thought I could remove the @Consumes(), as I'm setting the header myself, but then I get another error:

java.lang.RuntimeException: RESTEASY004590: You must define a @Consumes type on your client method or interface, or supply a default

The Content-Type will be different in each call I make depending on the document I send. Isn't there a way in RESTEasy to define it as a parameter?

1

1 Answers

0
votes

You can set the Consumes to the wildcard content type:

@POST
@Path("api/files/upload/")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes(MediaType.MEDIA_TYPE_WILDCARD)
FileUploadResponse uploadFile(byte[] file);

This will allow the method to accept any content type.

If it matters what type of content you're getting then do then in your method you can then examine the provided type:

@POST
@Path("api/files/upload/")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes(MediaType.MEDIA_TYPE_WILDCARD)
FileUploadResponse uploadFile(byte[] file, @Context HttpServletRequest request);

and in the implementation method:

String contentType = request.getHeader("Content-Type");