Announcement Announcement Module
Collapse
No announcement yet.
Cannot set character encoding using @ResponseBody and HttpMessageConverter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cannot set character encoding using @ResponseBody and HttpMessageConverter

    I have a number of controller methods that use the @ResponseBody annotation with a custom HttpMessageConverter defined in the context to write the result bean to the response.

    The content type of the response is set correctly but I can't seem to change the character encoding to UTF-8. Edit: just noticed that the content type is not being set either.

    The message converter I'm using:

    Code:
    public class ElementMessageConverter implements HttpMessageConverter<Element>
    {
       private static final MediaType MEDIA_TYPE = new MediaType( "application", "xml", Charset
                                                          .forName( "UTF-8" ) );
    
       public List<MediaType> getSupportedMediaTypes( )
       {
          return Collections.singletonList( MEDIA_TYPE );
       }
    
       public Element read( final Class<Element> class, final HttpInputMessage inputMessage )
                                                                                               throws IOException,
                                                                                               HttpMessageNotReadableException
       {
          try
          {
             InputStream inputBody = inputMessage.getBody( );
             SAXReader reader = new SAXReader( );
             Document doc = reader.read( inputBody );
    
             return doc.getRootElement( );
          }
          catch ( DocumentException de )
          {
             throw new HttpMessageNotReadableException( "Invalid request XML", de );
          }
       }
    
       public boolean supports( final Class<? extends Element> clazz )
       {
          return true;
       }
    
       public void write( final Element element, final HttpOutputMessage outputMessage )
                                                                                          throws IOException,
                                                                                          HttpMessageNotWritableException
       {
          HttpHeaders outputHeaders = outputMessage.getHeaders( );
          outputHeaders.setContentType( MEDIA_TYPE );
    
          OutputStream outputBody = outputMessage.getBody( );
          OutputStreamWriter outputWriter = new OutputStreamWriter( outputBody );
    
          element.write( outputWriter );
    
          outputWriter.close( );
       }
    }
    Looking at StringHttpMessageConverter and AbstractHttpMessageConverter they set the content type on the http headers in the same way that my code does.

    Stepping through the code using a debugger shows that the headers in the HttpOutputMessage are never used. The output message is only used to pass in the body output stream.

    Is there something I'm missing here? Our application needs to be able to return content in UTF-8 encoding and so far the only way I can see to do this is to pass the response into controller method and set it there (which defeats the point in using message converters).

    Any help would be appreciated.
    Last edited by stringbean; Oct 8th, 2009, 09:38 AM. Reason: Correction about content type

  • #2
    stringbean,

    One of the ways to explicitly set the encoding (as well as other headers) is to avoid using @ResponseBody for handling ajax responses. You can use ResponseEntity interface for that instead, have a look:

    Code:
    @RequestMapping(value="/some/url/", method=RequestMethod.GET)
    public ResponseEntity<String> handleAJAX() {
    		HttpHeaders responseHeaders = new HttpHeaders();
    		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
    		return new ResponseEntity<String>("actual output", responseHeaders, HttpStatus.CREATED);
    }

    Comment


    • #3
      @eugenep: That also helpt me! THX!

      Comment


      • #4
        you are welcome

        Comment


        • #5
          Originally posted by eugenep View Post
          stringbean,

          One of the ways to explicitly set the encoding (as well as other headers) is to avoid using @ResponseBody for handling ajax responses. You can use ResponseEntity interface for that instead, have a look:

          Code:
          @RequestMapping(value="/some/url/", method=RequestMethod.GET)
          public ResponseEntity<String> handleAJAX() {
          		HttpHeaders responseHeaders = new HttpHeaders();
          		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
          		return new ResponseEntity<String>("actual output", responseHeaders, HttpStatus.CREATED);
          }
          Thank you it helped me.

          BTW I've tried this and not worked:
          http://forum.springsource.org/showthread.php?t=81858

          Comment

          Working...
          X