aspose file tools*
The moose likes Spring and the fly likes setting content type for json response with @responsebody Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "setting content type for json response with @responsebody" Watch "setting content type for json response with @responsebody" New topic
Author

setting content type for json response with @responsebody

Amitosh Mishra
Ranch Hand

Joined: Feb 11, 2010
Posts: 49
Hi all,

i am having issue showing ® symbol in browser. it is shown as � .

from my controller i am returning the json string.

i tried to use ResponseEntity<String> and setting up content-Type but that is also not working.
please let me know how i can resolve the issue.

regards
Amitosh
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Amitosh Mishra wrote:Hi all,

i am having issue showing ® symbol in browser. it is shown as � .

from my controller i am returning the json string.

i tried to use ResponseEntity<String> and setting up content-Type but that is also not working.
please let me know how i can resolve the issue.

regards
Amitosh


I am confused. What do you mean tried to use ResponseEntity<String>

For setting the response body to be in json. You just annotate your return object with @ResponseBody. And have Apache Jackson jar in your classpath and <mvc:annotation-driven/> in your xml. That is all.

As far as characters showing in your browser. I am not sure about any limitation on the browsers with javascript to show that character. But that would not be a Spring issue. So you would probably want to ask in the html forum here instead for that.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Amitosh Mishra
Ranch Hand

Joined: Feb 11, 2010
Posts: 49
Hi Mark,

i was trying to set content type in response but i did not have any way to set that.
i googled that and some one suggested to use ResponseEntity<String> as return type and setting response headers in that.
but this was also not working.

now i have resolved this by putting HttpResponse in method arguments and instead of returning @ResponseBody writing the string on response.getWriter() and setting content type like HttpServletResponse.setcontenttype("argument").

can you suggest me how can i achieve same with @responsebody.


regards
Amitosh
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Well, you really aren't supposed to set it. It isn't the serverside's responsibility in RESTful Web Services. So therefore, unless you do it like you did, it happens automatically.

Basically, a major tenant of REST is that the client determines the format of the data that it wants. In the Request the client adds to the Request Header the format they want the data back. So if a request comes in asking for JSON, then Spring REST will automatically convert the Controller's returned object into JSON.

This is how I do it. On the client side JavaScript I state I want json, or xml, or html, and the same code on the Controller side works and returns exactly what the client asked for. using <mvc:annotation-driven" Spring automatically gives you a ContentNegotiatingViewResolver and HttpMessageConverter to do all the conversions. In these cases I just include the Apaceh Jackson jar file in my class path and all domain objects can be converted easily into json without me doing a thing.



Mark>
Amitosh Mishra
Ranch Hand

Joined: Feb 11, 2010
Posts: 49
Thanks for the info Mark..

but actually the problem was i had some special character like Chinese and copyright symbol which were not getting displayed in UI.
that's why i need to set character encoding in the response.

for the Json response we are doing exactly like you said but we don't have any ways to set character encoding until unless i am passing HttpResponse as method argument and modify the character encoding there.

regards
Amitosh
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1666
    
    7

The Json spec identifies the following as legal encodings:

UTF-8
UTF16
UTF-16LE
UTF-32BE
UTF-32LE

The default charset used with MappingJacksonHttpMessageConverter is UTF-8. However if one of the others listed is found on the Content-Type header it will be used instead.

If an application want to explicitly disregard Encoding limitations (to read in JSON encoded using an encoding not listed as allowed), they can use java.io.Reader/ java.io.Writer instances as input

You may should also always add the produces and consumes elements the @RequestMapping when it is applicable,

You can also try adding



to the top of you JSP

[How To Ask Questions][Read before you PM me]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting content type for json response with @responsebody