0

I am getting a response whose headers are :

Content-Length:18588

Content-Type:application/json;charset=UTF-8

Date:Wed, 02 Dec 2015 12:58:51 GMT

Server:Apache-Coyote/1.1

Is there a possibility that I change "application/json" encoded string to "text/plain;charset=ISO-8859-1" encoding before sending it to the view? I am using Spring MVC. Any help would be greatly appreciated.

Muhammad Umer
  • 363
  • 2
  • 8
  • 19
  • ISO in 2015? Why not use UTF-8? – fge Dec 02 '15 at 13:44
  • No, I want the controller to return it in application/json, but before sending it to the view, I want the manually change the response's content type to "text/plain". Response from api is application/json, which is correct, but I need to change the response to text/plain manually, due to some requirements. – Muhammad Umer Dec 03 '15 at 05:27

1 Answers1

1

Your requirement is to set the desired content type before sending response back to the client, so use httpServletResponse.setContentType("text/plain;charset=ISO-8859-1");

This is method from javax.servlet.ServletResponse(setContentType(java.lang.String type)

Below excerpt from doc.

Sets the content type of the response being sent to the client, if the response has not been committed yet. The given content type may include a character encoding specification, for example, text/html;charset=UTF-8. The response's character encoding is only set from the given content type if this method is called before getWriter is called.

This method may be called repeatedly to change content type and character encoding. This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed.

Containers must communicate the content type and the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the Content-Type header is used.

Please note: You should use this method before you commit the response back to the client. See this from above excerpt - "This method has no effect if called after the response has been committed. It does not set the response's character encoding if it is called after getWriter has been called or after the response has been committed."


On a side note:

  • To make your applicable more scalable i.e. possibility to support more languages in future, use encoding scheme as "UTF-8".
  • Do read my this answer to get in-depth details on WHY?
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70