cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergey Beryozkin <sergey.beryoz...@iona.com>
Subject Re: Altering JSON Output in a REST service
Date Thu, 29 Jan 2009 22:19:00 GMT

Hi

Have a look please here :
http://cwiki.apache.org/CXF20DOC/jax-rs-jsr-311.html

There's a section there dedicated to filters and interceptors and I hope
you'll find some 'recipe' there.
Most likely doing a CXF JAXRS ResponseFilter or customizing a default
JSONProvider (perhaps by extending it and just overriding writeTo(),
possibly still delegating to the superclass but writing the callback first
into your custom OutputStream buffer) should do the trick... 

Cheers, Sergey


shwetanaik wrote:
> 
> hi wildPengiun
> 
> can u plz explain more what u did to alter json output in rest service
> 
> I am, using jaxrs I also want to alter json output by adding callback
> function
> 
> please can provide some sample code
> 
> suppose my rest service is
> 
> @GET
>     @Produces("application/json")
>     @Consumes("application/json")
>     @Path("/users/json") 
>     public Flights getFlightListAsJson() {
>         return getFlights();    	
>            }
> 
> & i get ouput 
> {"flights":{"flight":[{"@flightId":"OK123","company":"Czech
> Airlines","number":123,"aircraft":"B737"},{"@flightId":"OK124","company":"Czech
> Airlines","number":124,"aircraft":"AB115"}]}}
> 
> but i wanted to do output like
> getflight({"flights":{"flight":[{"@flightId":"OK123","company":"Czech
> Airlines","number":123,"aircraft":"B737"},{"@flightId":"OK124","company":"Czech
> Airlines","number":124,"aircraft":"AB115"}]}})
> 
> how can write this getflight string in response using jaxrs.
> 
> 
> 
> wildPengiun wrote:
>> 
>> Thanks for that Sergey, but I've had a success with my REST
>> outInterceptors. Instead of using an annotation on the service class I've
>> added it to the beans.xml and now its burst into life. Now I can see the
>> content-type is JSON hoorah.
>> 
>> Next problem is how to change the message ?? any clues
>> 
>> Thanks for all the help.
>> 
>> 
>> 
>> Sergey Beryozkin-3 wrote:
>>> 
>>> Hi
>>> 
>>>>
>>>> I'm trying to alter some output in a REST call. I want to wrap the JSON
>>>> output with a callback name. I've managed to get a filter to work (all
>>>> my
>>>> attempts to get the OutInterceptor to work have failed). So now I've
>>>> got a
>>>> message object.
>>>>
>>>> To alter the message I thought I could use something like JSONStream os
>>>> =
>>>> message.getContent(JSONStream.class); but this returns null. Anyone
>>>> have an
>>>> idea how I can alter the JSON.
>>> 
>>> JAXRS Output filters are invoked immediately after the invocation has
>>> returned but before a given method response has been 
>>> serialized into the output message.
>>> 
>>> So I think what you need to do in your response handler is to replace
>>> the actual OutputStream.class with a buffered/filter one, 
>>> wrapping the original one.
>>> 
>>> What will happen next is that a JSONProvider will write into this stream
>>> and I guess at this moment  you can transfrom the incoming 
>>> output bytes.
>>> 
>>> It's basically doing same thing you can do with Servlet filters.
>>> 
>>> Default JSONProvider does not append a JSONStream.class.
>>> 
>>> As such another option is actually write a custom JSON
>>> MessageVBodyWriter, possibly either extending the default one or simple 
>>> copying its code and updating its wtiteTo() method to do a pre-request
>>> specific JSON or something like that.
>>> Providers can have @Contexts like UriInfo, etc, injected so one can get
>>> some additional info from there.
>>> 
>>> Does it make sense or am I missing the point ?
>>> 
>>> Cheers, Sergey
>>> 
>>>> -- 
>>>> View this message in context:
>>>> http://www.nabble.com/Altering-JSON-Output-in-a-REST-service-tp20478852p20478852.html
>>>> Sent from the cxf-user mailing list archive at Nabble.com.
>>>> 
>>> 
>>> 
>>> 
>>> 
>> 
>> 
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Altering-JSON-Output-in-a-REST-service-tp20478852p21737234.html
Sent from the cxf-user mailing list archive at Nabble.com.


Mime
View raw message