Reputation: 184
I simply want to pass an map to my @GET method on my web services using JAX-RS 2. I want to know if it's possible and how to make it, even if GET's methods don't have any body.
Like this :
@GET
@Path(??)
@Produces({"application/xml", "application/json"})
public User find(@PathParam(??) HashMap<String, String> map) {
// work on map !
}
I've seen that it existed the MultiValued map but it's for the parameters. How to pass an map please ?
Thanks by advance !
Upvotes: 3
Views: 8887
Reputation: 2416
Get comfortable with @Context.
@GET
public Response superSpecialSearch(@Context UriInfo info, @Context HttpHeaders headers, @Context SecurityContext securityContext) {
var iAmAMutivalueMap = info.getQueryParameters(true);
var searchResults = searchForThings( iAmAMultivalueMap );
return Response.ok(searchResults )
}
Upvotes: 0
Reputation: 45745
You will need to have Jackson jars in your classpath (or any other XML / JSON to Map mapper)
You probably don't want to pass the map on the @PathParam
, for aesthetical, convention and security reasons. You usually pass a JSON / XML object as the request body, e.g. using a POST / PUT
@POST
@Path("/anypath")
@Consumes({"text/xml", "application/json"})
public User find(HashMap<String, String> map) {
//magic should happen
}
Then just pass a POST / PUT request with content type application/json or text/xml that has e.g.
{
"key1": "value1"
"key2": "value2"
}
If you have the right Jackson / Moxy etc mapper in the classpath, it will do the conversion between the JSON / XML format to a java.util.Map
(or even a POJO) for you
The @Produces
is only needed if you intend to also return XML / JSON, but since you are expecting either XML or JSON, then having a @Consumes
makes sense here.
However, if you must pass the JSON object on a GET request, e.g. as a request param, take a look at this question: Convert JSON query parameters to objects with JAX-RS
p.s. for xml the mime is text/xml
and not application/xml
Upvotes: 3