开发者

How to get a Map parameter in a REST service for different mime types?

How can I specify a map as one of the parameters of a REST service e.g

@Path("/servicepath")
@Consumes(MediaType.APPLICATION_XML)
public class MyResource {
    @POST
    public Response getMap(Map<String, List<Object>>) {
    //code here
    }
}

or

@Path("/servicepath")
@Consumes(MediaType.APPLICATION_JSON)
public class MyResource {
    @POST
    public Response getMap(Map<String, List<Object>>) {
    //code here
    }
}

I am using Jersey. Should I implement a MessageBodyReader for that? But i开发者_如何转开发mplementing a reader for a generic type like Map seems a bad way for me. Maybe I should write a wrapper class on top of the Map object.

What is your ideas? Thanks.


The JAX-RS specification (section 4.2.4) does require that implementors (like jersey) provide a MessageBodyReader implementation for MultivaluedMap<String, String> which is used to consume application/x-www-form-urlencoded mime types. So for example, you can do something like this:

@Path("/servicepath")
@POST
@Consumes("application/x-www-form-urlencoded")
@Produces("text/plain")
public String doTheFormThing(MultivaluedMap<String, String> formdata) {
    return formdata.toString();
} 

Is this not sufficient for what you're trying to do?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜