users@glassfish.java.net

How to customize a JSON response?

From: <glassfish_at_javadesktop.org>
Date: Wed, 06 Aug 2008 10:22:47 PDT

I am working with the following set of requirements

1. A GET request will be generated by a <script src="[i]restful-service-url[/i]"> tag in a widget.
2. The returned mime-type shall be "application/x-javascript".
3. The response body will contain a JSON serialization of data wrapped in a javascript call to be specified in the query string.
4. If an error occurs on the server, the information about the error will be returned as a JSON object, with a 200 response code header.
5. The solution must be a modification of an existing service that responds to GET requests for mime-type "application/xml"

So, I am working from the JsonFromJaxb sample application and trying to understand the requirements imposed by the framework in order to accomplish the task. As I understand it, I need to:

1. Annotate a resource that returns a response type so that a 200 response code can be returned and a custom body inserted in case of an error.
2. Create a ResponseBuilder object.
3. Insert the JSON serialization of the requested object SURROUNDED BY JAVASCRIPT FUNCTION CALL into the response body (or error-message JSON in a catch block)
4. Set the headers.
5. Return the results of the build() method of the ResponseBuilder.

I have tried a number of things, running into various difficulties related to not knowing what is possible at different points within the context provided by the framework.

SO: suggestions, please. How best to accomplish this?
[Message sent by forum member 'snyderpa' (snyderpa)]

http://forums.java.net/jive/thread.jspa?messageID=291935