Not sure I understand your question, the content type of the response is defined by the server? What is it you are trying to achieve?
Some browsers (such as IE) will try to perform content type sniffing if none is defined in the response. It's very difficult to tailor this behaviour, you would need to update a whole bunch of stuff in the registry to do it.
Paul Sturrock wrote:Not sure I understand your question, the content type of the response is defined by the server? What is it you are trying to achieve?
The requirement is that the clients can ask for the JSON or the XML version of a record or a set of records using a GET request. So i was thinking to set the content-type requested by the GET request so that the servlet can convert the data object into either JSON or XML.
Normally, when from a browser, we do a http://localhost:8080/appName/servletName, its a GET request but no content-type is set.
However the same can be set through a Java client to make a GET request like
and the same can be obtained in the servlet as
So, i was wondering if there was a way to set this property from the browser(but not a request parameters)
If you want the server to produce one of two formats based on something in the request, the obvious thing to do is to pass a parameter defining the format. This has the advantage that you don't have to do weird things like hacking the browser to make it work, it is just a standard feature of HTML.
I am using Jersey framework for this and eventually i want the requests like foo.json or foo.xml to go to a single method in the Servlet, but i am not sure whether if my urls are different in these cases(foo.json or foo.xml), how will jersey handle the request and how will i read the ".json" part.
Also, i think Jersey provides an annotated way of handling both xml and json in the same method using jaxb but i am not using it because of some other related complexities. so i am using a custom way to convert object to xml or json
i have a large no of methods written for returning the XML type data, and repeating the code for all the methods just because of a different type of data conversion doesn't look good to me.
Because after some other time, a different content type requirement might come, and i dont want to rewrite all the methods just because the content type is different(unless the business logic requires some different processing for different content type)
So, what i am having right now, is that i get my data object from the backend to the jersey servlet and then based on the request parameter convert it to either JSON or XML(and that code is common).
Also, it looks like with Jersey, we can specify 2 or more content types for the same method, and that seems to be ok for what i am trying to do.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop