users@jersey.java.net

[Jersey] Re: JSON with JAXBContextResolver and JAXB

From: John Yeary <johnyeary_at_gmail.com>
Date: Fri, 3 Aug 2012 22:46:40 -0400

I have not looked at the specification so I can't answer that one honestly.
Tatu though followed-up with the assist.

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 Fri, Aug 3, 2012 at 2:45 AM, James Green <james.mk.green_at_gmail.com>wrote:

> John,
>
> I wrapped up the contents of the constructor in a try/catch to
> specifically log what was happening but nothing was logged.
>
> In the end I commented out all the classes for binding and the constructor
> was finally invoked.
>
> Then I was forced to uncomment the classes until I found the constructor
> no longer called. As you can imagine, I was swearing at the screen by this
> time.
>
> Anyway I now have the classes loaded having removed a few unnecessary ones
> and fixed a few others.
>
> My next job will be to make this work beyond Arquillian testing, into a
> real deployment to Glassfish, and to JBoss.
>
> Now, before I sign off I want to return the root of the problem: the lack
> of a cross-container default JSON notation. If it was not for this lack of
> standardisation I would not be wasting my time this week. My boss doesn't
> get why I'm having to put this time at all, after-all "Java must do JSON by
> now". So, I've heard there's a "future spec" to address this point, what
> news is there of it? I am specifically wanting to ensure that my code today
> does not rot into the future.
>
> James
>
> On 2 August 2012 21:27, John Yeary <johnyeary_at_gmail.com> wrote:
>
>> I was looking at your response around loading all of the classes. Perhaps
>> you can get the list of classes to be bound from JAXB and provide them to
>> the JSONJAXBContext.
>>
>> 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 9:03 AM, James Green <james.mk.green_at_gmail.com>wrote:
>>
>>> John,
>>>
>>> Maven writes out a list of classes to be bound to within the jaxb.index
>>> file. This therefore seems rather redundant?
>>>
>>> If I were to have several dozen classes (or even hundreds), would this
>>> not be rather too cumbersome? I followed something on StackOverflow that
>>> said the constructor would take a String named package too.
>>>
>>> James
>>>
>>> On 2 August 2012 13:37, John Yeary <johnyeary_at_gmail.com> wrote:
>>>
>>>> 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
>>>>>
>>>>>
>>>>
>>>
>>
>