• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

setting content type for json response with @responsebody

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Amitosh Mishra
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was facing same issue and fixed it by setting header with charset=utf-8 and it worked -



And for responseBody can refer https://stackoverflow.com/questions/27599620/spring-controller-responsebody-text-xml-response-utf-8-encoding-issue
 
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch, Sanchi!
reply
    Bookmark Topic Watch Topic
  • New Topic