users@glassfish.java.net

Re: How to customize a JSON response?

From: <glassfish_at_javadesktop.org>
Date: Wed, 06 Aug 2008 16:51:22 PDT

I'm just going to take a wild guess about what you're actually asking here, it's a bit muddled.

I'm assuming that you have the source code to the service, rather than fronting it with your own service, in truth it's not really important.

If you're asking about converting XML to JSON using JAXB, I can't help you, no idea.

If you're asking how to send a "200" and a proper JSON response due to an error, why is this more difficult than:
[code]

@HttpMethod("GET")
@UriTemplate("/scoobysnacks")
@ProduceMime("application/x-javascript")
public String scoobySnacksMaker() {
    try {
        String jsonResult = convertToJson(mmmGimmeScoobySnackInXML());
        return jsonResult;
    }
    catch (Throwable t) {
        String jsonErrorResponse = formatProperJsonResponseBasedOnException(t);
        return jsonErrorResponse;
    }
}
[/code]

If you're looking to catch ALL errors and wrap them up in JSON (regardless of the service), then I'd write a Filter and handle that there.

Finally, as a matter of style and idiom, if you're actually, you know, trying to actually apply REST over HTTP (rather than just ad hoc send and get stuff over HTTP), then you should return legit HTTP response codes, and your Javascript client should be processing THOSE correctly rather than relying on the server.

A 200 result means "Okey Dokey", and if you're sending 200 when it's really not, you know, "Okey Dokey", then that kind of goes against the spirit of leveraging the HTTP protocol that one aspect of REST advocates. Mind, you could still send a JSON payload in your errors, but you shouldn't use a 200 result code.

But, like I said, if you're just doing ad hoc "forget the standards and practices, I just need a socket, thanx" services over HTTP, then do whatever the heck you want.

Feel free to elaborate if I missed your point.
[Message sent by forum member 'whartung' (whartung)]

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