users@jersey.java.net

[Jersey] Re: CustomResolver (Marshaller) dual content type xml and json

From: Mohan KR \(mkannapa\) <"Mohan>
Date: Fri, 22 Apr 2011 07:43:48 -0500

*bump*

Apologize for bumping it, seeing if anyone had experience this? Some more
details here is my CustomResolver. A few things

I have tried:

  * Different JAXB impl : a) The one bundled with JDK1.6 b) Metro JAXB
2.1.13 c) Moxy 2.2.0

It does not matter, JSON is not being generated, and there are no logs that
i could see to tell where I'm going wrong.

If this is not the correct approach would appreciate any pointers in the
right direction.

 

 

@Provider

public class ContextResolverMarshaller

        implements ContextResolver<Marshaller> {

    

    @Override

    public Marshaller getContext(Class<?> type) {

        if (type == null) {

            return null;

        }

        

        //We will create the JSONJaxbContext, so we can support both XML and
JSON

        JSONJAXBContext ctxt = null;

        // create a brand spanking new one, note I have tried all the
different JSON

        // configurations, to no avail

        try {

            ctxt = new
JSONJAXBContext(JSONConfiguration.badgerFish().build(), type);

        }

        catch (final JAXBException ex) {

            return ctxt;

        }

        

        Marshaller m = null;

        try {

            m = ctxt.createMarshaller();

        }

        catch (final JAXBException ex) {

            return m;

        }

        // set our properties

        this.setProperty(m, Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        this.setProperty(m, Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        return m;

    }

 

    private void setProperty(Marshaller m, String name, Object value) {

        try {

            m.setProperty(name, value);

        }

        catch (final PropertyException ex) {

              //log

        }

    }

}

 

 

Thanks!

Regards,

 

Mohan KR

 

From: Mohan KR (mkannapa) [mailto:mkannapa_at_gmail.com]
Sent: Wednesday, April 20, 2011 7:19 PM
To: users_at_jersey.java.net
Subject: CustomResolver (Marshaller) dual content type xml and json

 

Greetings,

  My core problem is I need to support XML and JSON in my responses. I have
JAXB annotated POJOS, and

  my resource method's have the appropriate @Produces Annotation. With this
setup everything appears

  to be working fine, using curl to send separate requests with the
appropriate Accept headers.

 

  After, making sure things were working, I need to set some Marshaller
Properties, and it appears that the

  recommended approach is to use the ContextResolver<Marshaller> approach. I
did that and registered it

  as a provider to the runtime. Now, XML content type is working fine, but
JSON is broken I get no content.

  Do I need to implement a JSONMarshaller too? Although my ContextResolver
is being called when I make

  the request with application/xml and application/json. Don't know whether
it is a bug or maybe I'm missing

  something.

 

  I'm using jersey 1.6. Thanks in advance.

 

 

Thanks!

Regards,

 

Mohan KR