user4499364
user4499364

Reputation:

How to read a response from a web service that returns a byte[]

I've got this code of my web service:

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public static Response fillData(final Map<String, Object> data, final   String name, @Context final ServletContext context) {  
       ...  
       final byte[] file = ...
       return Response.ok(file).build();

How can I read the response in my client?

final javax.ws.rs.core.Response reponse = client.target(URL_REST).path("/path").request(MediaType.APPLICATION_JSON).get();

This is totally wrong (ByteArray doesn't exist), but I want to do something like it:

byte[] pdfByteArray = reponse.readEntity(ByteArray.class);

How can I do?

Upvotes: 10

Views: 14700

Answers (2)

user4499364
user4499364

Reputation:

Like suggested Gabor above, I'll just use byte[].class Thanks!

Upvotes: 6

mp911de
mp911de

Reputation: 18119

Use java.io.InputStream

InputStream is = reponse.readEntity(InputStream.class);

This allows you to read in a streaming way instead of reading all bytes at once.

Upvotes: 17

Related Questions