users@glassfish.java.net

Re: how to switch between XML and JSON ?

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 05 Jan 2009 13:52:29 +0100

On Jan 3, 2009, at 5:07 PM, Felipe Gaścho wrote:

> humm.. so, If I declare both languages, the client request will define
> which one should be used in the response.. .. interesting.....
>
> is there a way to establish a default ? the order ? or other...
>

If the client does not send an Accept header then XML will be the
default because it is declared before JSON in your example.

Jersey does have support for mapping URI suffixes to an Accept header
if you want to support say ".xml" or ".json" to return in the media
type you like without the client declaring an Accept header. You need
to do something like this:

   package foo;

   public class MyConfig extends PackagesResourceConfig {
     public MyConfig() {
       super("foo.resources");

       getMediaTypeMappings().put("xml",
MediaType.APPLICATION_XML_TYPE);
       getMediaTypeMappings().put("json",
MediaType.APPLICATION_JSON_TYPE);
     }
   }

and then register the class in the web.xml as an init-param as follows:

         <init-param>
             <param-name>javax.ws.rs.Application</param-name>
             <param-value>foo,MyConfig </param-value>
         </init-param>

See:

   https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.1/api/jersey/com/sun/jersey/api/core/DefaultResourceConfig.html
#getMediaTypeMappings()

   https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.1/api/jersey/com/sun/jersey/api/core/PackagesResourceConfig.html

   https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.1/api/jersey/com/sun/jersey/spi/container/servlet/ServletContainer.html


Paul.

> On Sat, Jan 3, 2009 at 4:39 PM, heapifyman <heapifyman_at_gmail.com>
> wrote:
>>
>> The client has to set the ACCEPT header to JSON and only JSON, I
>> suppose.
>>
>> Am 28.12.2008 um 17:41 schrieb Felipe Gaścho:
>>
>>> I have a method annotated to produce both XML or JSON contents..
>>> how
>>> do I switch between them ? what is the URL suffix I must add to my
>>> resource URL ??
>>>
>>> For example:
>>>
>>> http://fgaucho.dyndns.org:8080/footprint-service/admin/config/123
>>>
>>> This returns an XML (because the JAXBElement is being automatically
>>> unmarshalled to XML ).. how do I read it as JSON string ??
>>>
>>> * the fragment of code I am using as example:
>>>
>>> @GET
>>> @Produces( { MediaType.APPLICATION_XML,
>>> MediaType.APPLICATION_JSON
>>> })
>>> public JAXBElement<FootprintConfig>
>>> get(@PathParam("config_id")
>>> String id) {
>>> JAXBElement<FootprintConfig> jbe =
>>> factory.createConfiguration(factory
>>> .createFootprintConfig());
>>> return jbe;
>>> }
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>>
>>
>>