Vijay
Vijay

Reputation: 597

Jersey:Returning a Response with a Map containing Image Files and JSON String values

I am using Jersey JAX-RS. I want to return a Response with a Map containing Image Files and JSON String values.

Is this the right way to do this:

Map<String,Object> map = new HashMap........


GenericEntity entity = new GenericEntity<Map<String,Object>>(map) {};

return Response.ok(entity).build();

Or is this better.I plan to use JAX-RS with Jersey only.

JResponse.ok(map).build();

I am basing this on this article:

http://aruld.info/handling-generified-collections-in-jersey-jax-rs/

I am not sure what to specify for @Produces too(planning to leave it out).

TIA,

Vijay

Upvotes: 1

Views: 2909

Answers (1)

yves amsellem
yves amsellem

Reputation: 7234

You better produce a multipart response:

import static com.sun.jersey.multipart.MultiPartMediaTypes.MULTIPART_MIXED_TYPE;
import static javax.ws.rs.core.MediaType.APPLICATION_XML_TYPE

@GET 
@Produces(MULTIPART_MIXED_TYPE) 
public Response get() 
{ 
  FileDataSource image = ... (gets the image file) 
  String info = ... (gets the xml structured information) 

  MultiPart multiPart = new MultiPart(). 
  bodyPart(new BodyPart(info, APPLICATION_XML_TYPE)). 
  bodyPart(new BodyPart(image, new MediaType("image", "png"))); 

  return Response.ok(multiPart, MULTIPART_MIXED_TYPE).build(); 
}

This example was taken from there.

Upvotes: 2

Related Questions