users@jersey.java.net

Re: [Jersey] How to reuse JAXB config in a toString method ?

From: Bruno Vernay <brunovern.a_at_gmail.com>
Date: Tue, 12 May 2009 14:13:24 +0200

It works

@Override
public String toString() {
  Client c = Client.create();
  Providers ps = c.getProviders();
  MessageBodyWriter mbw = ps.getMessageBodyWriter( CamembertVO.class,
CamembertVO.class, null, MediaType.APPLICATION_JSON_TYPE);

  OutputStream out = new ByteArrayOutputStream();
  try {
        mbw.writeTo( this, CamembertVO.class, CamembertVO.class, null,
MediaType.APPLICATION_JSON_TYPE, null, out);
  } catch (WebApplicationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
  } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
  }
  return out.toString();
}

 thanks
Bruno


On Tue, May 12, 2009 at 11:24 AM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
> Hi Bruno,
>
> I just realized there is another way, you can reuse the Providers [1]
> supplied by the client, then the same JSON/JAXB you have used for obtaining
> the JAXB instance can be used.
>
>  Client c = ...
>  Providers ps = c.getProviders();
>  MessageBodyWriter mbw = ps.getMessageBodyWriter(
>      MyJAXBObject.class, MyJAXBObject.class,
>      new Annotaton[0],
>      MediaType.APPLICATION_JSON_TYPE);
>
>  OutputStream out = ...
>  MyJAXBObject o = ...
>  mbw.writeTo(
>      o,
>      MyJAXBObject.class, MyJAXBObject.class,
>      new Annotaton[0],
>      MediaType.APPLICATION_JSON_TYPE,
>      new OutBoundHeaders(),
>      out);
>
>
> The alternative is to utilize the JSON/JAXB stuff directly as you indicate
> but this is currently tricky. Jakub is working on cleanly layering this so
> it can be reusable.
>
> Paul.
>
> [1]
> https://jersey.dev.java.net/nonav/apidocs/1.1.0-ea/jersey/com/sun/jersey/api/client/Client.html#getProviders()
>
> On May 12, 2009, at 11:08 AM, Bruno Vernay wrote:
>
>> Sorry no, it is simpler than that.
>> I have a simple Value Object and I want a toString method that would
>> return a JSON representation.
>>
>> As I continue to search it looks like I could get a marshaller from a
>>
>> A JSONConfiguration
>>
>> [https://jersey.dev.java.net/nonav/apidocs/1.0.3/jersey/com/sun/jersey/api/json/JSONConfiguration.html]
>> can be use to construct a JSONJAXBContext  and then I could get a
>> marshaller
>>  [https://jersey.dev.java.net/nonav/apidocs/1.0.3/jersey/com/sun/jersey/api/json/JSONJAXBContext.html#createMarshaller()]
>> then I could use it to marshall my object
>>
>> [https://jaxb.dev.java.net/tutorial/section_4_5-Calling-marshal.html#Calling%20marshal]
>>
>> The only point is that I have to create the same JSONConfiguration
>> with my code and with the object's annotation.
>>
>> Is it the right direction ??
>>
>> Bruno
>>
>>
>>
>> On Tue, May 12, 2009 at 10:39 AM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
>>>
>>> Hi Bruno,
>>>
>>> The unmarshalling process from a JSON document to a JAXB object may
>>> result
>>> in syntactic character loss when the JAXB object is marshalled back to a
>>> JSON document (plus when unmarshalling any unknown JSON objects will be
>>> ignored and thus will not appear in the marshaled JSON document).
>>>
>>> So it is not possible to guarantee the exact result as from Jersey, in
>>> terms
>>> of characters received. Your solution will not guarantee that either
>>> because
>>> a subsequent result in something different because something else might
>>> have
>>> changed the state of the resource).
>>>
>>> Do you want to utilize the toString method for logging/debugging
>>> purposes?
>>> If so you can use the logging filter:
>>>
>>>
>>>  https://jersey.dev.java.net/nonav/apidocs/1.1.0-ea/jersey/com/sun/jersey/api/client/filter/LoggingFilter.html
>>>
>>> create an instance of LoggingFilter and add it to the filters of the
>>> WebResource you use to get the JAXB object. You can write your own filter
>>> to
>>> buffer the response entity as a String, by copying the code from the
>>> logging
>>> filter.
>>>
>>> Paul.
>>>
>>> On May 12, 2009, at 10:23 AM, Bruno Vernay wrote:
>>>
>>>> Hi,
>>>>
>>>> I use Jersey-client and JAXB to consume JSON services.  I use
>>>> Jersey-server to test the client via JUnit (followed and simplified
>>>> this tip:
>>>>
>>>> http://blog.tmro.net/2009/03/unit-test-jax-rs-using-java-6-and-junit.html).
>>>>
>>>> It went fine, just a little problem with JAXB and immutable objects.
>>>> Now I am really stuck with this problem:
>>>>
>>>> I would like to implement a toString method for my object, that would
>>>> give the same exact result as Jersey. i.e. uses JAXB with the same
>>>> configuration and parameters as Jersey.  But I didn't find any code
>>>> showing how to do it. I ended with this silly solution:
>>>>
>>>> @Override
>>>> public String toString(){
>>>>  Client c = Client.create();
>>>>  WebResource r = c.resource("http://localhost:9998/myModule/meObject");
>>>>  return (String) r.get(String.class);
>>>>  //return "RefVO ("+this.maName + " ; "+this.versionId+" ;
>>>> "+this.lastModifiationDate.toString()+")";
>>>> }
>>>>
>>>>
>>>> Regards
>>>>
>>>> --
>>>> Bruno VERNAY
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>>
>>
>>
>>
>> --
>> Bruno VERNAY
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>



-- 
Bruno VERNAY