users@jersey.java.net

[Jersey] Re: JSON with JAXBContextResolver and JAXB

From: John Yeary <johnyeary_at_gmail.com>
Date: Thu, 2 Aug 2012 08:37:30 -0400

Hello James,

The code looks fairly good, but ht e JSONJAXBContext takes a
JSONConfiguration, and an array of classes to be bound to. You should have
something like...

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JAXBContextResolver implements ContextResolver<JAXBContext> {

    private final JAXBContext context;
    private final Set<Class> types;
    private final Class[] classesToBeBound = {Account.class};
    public JAXBContextResolver() throws JAXBException {
        System.out.println("Creating a custom JAXBContextResolver");
        this.types = new HashSet(Arrays.asList(classesToBeBound));
        this.context = new JSONJAXBContext(JSONConfiguration.natural().build(),
classesToBeBound);
        System.out.println("Created a custom JAXBContextResolver");
    }

    @Override
    public JAXBContext getContext(Class<?> type) {
        System.out.println("Servicing context request for type " +
type.getSimpleName());
        return types.contains(type) ? context : null;
    }

}

I hope that helps...

John
____________________________

John Yeary
____________________________
*NetBeans Dream Team*
*President Greenville Java Users Group
Java Users Groups Community Leader
Java Enterprise Community Leader*

____________________________

<http://javaevangelist.blogspot.com/> <https://twitter.com/jyeary>
<http://www.youtube.com/johnyeary>
  <http://www.linkedin.com/in/jyeary>
<https://plus.google.com/112146428878473069965>
  <http://www.facebook.com/jyeary>
<http://feeds.feedburner.com/JavaEvangelistJohnYearysBlog>
  <http://netbeans.org/people/84414-jyeary>

"Far better it is to dare mighty things, to win glorious triumphs, even
though checkered by failure, than to take rank with those poor spirits who
neither enjoy much nor suffer much, because they live in the gray twilight
that knows not victory nor defeat."
-- Theodore Roosevelt



On Thu, Aug 2, 2012 at 6:25 AM, James Green <james.mk.green_at_gmail.com>wrote:

> Hi,
>
> Am trying to follow
> http://jersey.java.net/nonav/documentation/latest/json.html#json.jaxb.approach.section
>
> On my first attempt I was told that no ObjectFactory or jaxb.index file
> exists. So following a suggestion on StackOverflow I added a
> maven-antrun-plugin to my POM which generated my jaxb.index file (there are
> quite a few models with JAXB annotations).
>
> However, the problem persists. Perhaps a classpath issue, but I've not
> found any examples to follow to investigate and fix.
>
> The root of my problem here is that I am building a project for deployment
> in either Glassfish or JBoss. Under testing we found the JSON
> representations using shipped defaults differed making testing impossible.
> I was advised to check the JSON Processing spec mailing list by the London
> JUG for advice on overcoming this, but was told there's nothing in the
> pipeline and to ask here instead.
>
> So in short I am trying to specify JSON.NATURAL across both Glassfish and
> JBoss, beginning with the link above and still not getting far.
>
> Some help and insight would be appreciated.
>
> Here's my Provider:
>
> @Provider
> @Produces(MediaType.APPLICATION_JSON)
> public class JAXBContextResolver implements ContextResolver<JAXBContext> {
>
> private JAXBContext context;
> public JAXBContextResolver() throws JAXBException {
> System.out.println("Creating a custom JAXBContextResolver");
> this.context = new
> JSONJAXBContext(JSONConfiguration.natural().build(),
> com.example.model.Account.class.getPackage().getName());
> System.out.println("Created a custom JAXBContextResolver");
> }
>
> @Override
> public JAXBContext getContext(Class<?> type) {
> System.out.println("Servicing context request for type " +
> type.getSimpleName());
> return this.context;
> }
>
> }
>
>
> James
>
>