users@jersey.java.net

Re: [Jersey] User controlled response

From: Marc Hadley <Marc.Hadley_at_Sun.COM>
Date: Tue, 10 Nov 2009 15:29:49 -0500

> Paul hinted at another mechanism:
>
> Will this work for returning the "accepted" response format?
>
> @GET
> @Produces("text/plain")
> @Path("{id}")
> public String returnText(...) {
> }
>
> @GET
> @Produces("application/json")
> @Path("{id}")
> public String returnJson(...) {
> }
>
Yes, that should work.

Marc.

>
> Paul Sandoz wrote:
>>
>> On Nov 10, 2009, at 12:11 AM, Louis Polycarpou wrote:
>>
>>> I have a related requirement to David's. If I have a list of mime
>>> types that my resource can consume, how do I find out from within
>>> my resource method which mime-type Jersey 'accepted' in order to
>>> fire the current method?
>>>
>>> @GET
>>> @Consumes("text/plain,application/json")
>>
>> The above should be:
>>
>> @Consumes({"text/plain", "application/json"})
>>
>>
>>> @Path("{id}")
>>> public String getId(...) {
>>> // if I'm consuming JSON do this...
>>>
>>> // else if it's plain text do this...
>>> }
>>>
>>
>> Marc's reply shows how you can get the selected media type and then
>> you can compare that media type.
>>
>> Depending on what you are doing it might be easier to separate out
>> your methods:
>>
>> @GET
>> @Consumes("text/plain")
>> @Path("{id}")
>> public String getText(...) {
>> }
>>
>> @GET
>> @Consumes("application/json")
>> @Path("{id}")
>> public String getJson(...) {
>> }
>>
>> It is possible to avoid duplication of @Path by using a sub-
>> resource locator. If you want to share state with the parent
>> resource that class can be a non-static public inner class, so one
>> could do:
>>
>> @Path("{id}")
>> public Inner getId(...) {
>> return new Inner();
>> }
>> public class Inner {
>> @GET
>> @Consumes("text/plain")
>> public String getText(...) {
>> }
>>
>> @GET
>> @Consumes("application/json")
>> public String getJson(...) {
>> }
>> }
>>
>> Paul.
>>
>>
>>> Thanks,
>>> Louis
>>>
>>> On 9 Nov 2009, at 23:02, Marc Hadley wrote:
>>>
>>>> On Nov 9, 2009, at 4:37 PM, dloy wrote:
>>>>
>>>>> I want to create the response format (i.e. JSON, XML, ...) in
>>>>> Jersey. Does Jersey provide a mechanism for a program to capture
>>>>> the accepted Mimetype, as determined by Jersey, without Jersey
>>>>> creating the response format.
>>>>>
>>>> If I understand the question correctly. You can use
>>>> Request.selectVariant[1] to have Jersey pick the best matching
>>>> alternative from a set of available formats that you specify
>>>> using a list of Variants. E.g. if you supported JSON and XML for
>>>> a particular resource you can write something like:
>>>>
>>>> @Context Request request;
>>>>
>>>> List<Variant> variants =
>>>> Variant.mediaTypes(MediaType.APPLICATION_XML_TYPE,
>>>> MediaType.APPLICATION_JSON_TYPE).add().build();
>>>> Variant selected = request.selectVariant(variants);
>>>> MediaType selectedType = selected.getMediaType();
>>>>
>>>> The above will look at the HTTP Accept header in the request and
>>>> determine the best match out of the list you provide.
>>>>
>>>> HTH,
>>>> Marc
>>>>
>>>> [1] https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/core/Request.html
>>>> #selectVariant(java.util.List) <https://jsr311.dev.java.net/nonav/releases/1.0/javax/ws/rs/core/Request.html#selectVariant%28java.util.List%29
>>>> >
>>>>
>>>>> Thanks
>>>>> David
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net <mailto:users-unsubscribe_at_jersey.dev.java.net
>>>>> >
>>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net <mailto:users-help_at_jersey.dev.java.net
>>>>> >
>>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net <mailto:users-unsubscribe_at_jersey.dev.java.net
>>>> >
>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net <mailto:users-help_at_jersey.dev.java.net
>>>> >
>>>>
>>>
>>>
>>> ------------------------------------------------------------------------
>>> Louis Polycarpou /(founder)/
>>>
>>> <adjoovo-email.png>
>>> Adjoovo
>>> SERVICE-ORIENTED COLLABORATION
>>> http://adjoovo.com
>>> http://twitter.com/adjoovo
>>> +44 (0) 7786 136653
>>>
>>> Louis Polycarpou
>>> louis_at_adjoovo.com <mailto:louis_at_adjoovo.com>
>>>
>>>
>>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>