Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning the different type from the Servlet Filter  RSS feed

 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I have an application in which REST is used to return data to the client.

The data format could be an XML String or a JSON String.

Normally one would use JAXB for the conversion of Object -> XML/JSON but because of certain issues, we are not using it.

So eventually we have a data object at the REST layer and we need to convert it to XML/JSON. We already have a Serializer in place that can convert the data object to JSON/XML.

The problem is where do we convert the object -> XML/JSON.

One option is to convert the dataobject to the desired format in all the methods of the Servlet(REST Servlet) and return a String.

But i was thinking if we could return the data object from the REST and probably have a filter in which we could convert the data object to String and returns to the server

Is there a way to accomplish this(ie to return a different data type from the filter).

Or can anyone please suggest an alternate way?

Thank you

Rahul
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The convention is for the REST servlet to detect the requested format and handle formatting of the resource. Why would you want anything different? Sounds like needless complication to me.

Bill
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,

Thanks for the reply.

Yes, i think you are right, i realized sometime back that changing the contents to be returned in the filter will be an extra complication that we can avoid.

Thank you

Rahul
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems like it'd be a good place to introduce a controller servlet that shuffles the request off to an appropriate handler. The handler returns the object, the servlet knows what return type the request needs, and converts the object appropriately. If I was at that stage, though, I'd already be looking into any number of existing frameworks that can handle some of that for me.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!