Hi

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.

This is great !

Now I can see the content-type is JSON hoorah.

Next problem is how to change the message ?? any clues

1. One approach is to have an output stream in the output message temporarily replaced. This code will work if you do a ResponseHandler filter or CXF interceptor :

message.setContent(OutputStream.class, new 
JSONOutputStream(message.getContent(OutputStream.class)));

In this JSONOutputStream you can convert the json-formatted data as needed, 
data will already be coming in a JSON format.

For this to work it has to be done immediately after a method invocation returns but before *before* JAXRSOutInterceptor is invoked. If you do ResponseHandler filter then it will work as is, if you do CXF interceptor then it will have to be placed before JAXRSOutInterceptor which sits on MARSHALL phase.

2. Another approach is to modify/replace the actual response object and let 
JAXRSOutInterceptor serialize it:

MessageContentsList objs = MessageContentsList.getContentsList(message);
// this object is what has been returned from a method, to be serialized later 
on
Object responseObj = objs.remove(0);
objs.add(replaceWithMyObject(responseObject));

Comments about the ordering apply here too.

3. Finally,  you can write a more advanced JSON MessageBodyWriter which will do 
custom serialization on a per-user request.
I'll need to document it all

4. If your interceptor has to sits after JAXRSOutInterceptor for some reason then it might still be possible to do. You'll need to take the warpped output stream in the output message (one of HTTP conduits does this wrapping) by flushing it first into a buffer, and replace it with your input stream - I'm not sure if it will actually work well though but it might

Hope it helps
Sergey



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-tp20478852p20480532.html
Sent from the cxf-user mailing list archive at Nabble.com.




Reply via email to