Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@ResponseBody doesn't obey CharacterEncodingFilter [SPR-6443] #11109

Closed
spring-issuemaster opened this Issue Nov 24, 2009 · 9 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

spring-issuemaster commented Nov 24, 2009

Morten Egelund Rasmussen opened SPR-6443 and commented

I have a @Controller class defined as a REST webservice endpoint. It has a method, which is annotated with @ResponseBody.

Class looks something like this:

@Controller
@RequestMapping("/foo")
public class Foo {
    @RequestMapping(value="/foobar", method=RequestMethod.GET)
    @ResponseBody
    public string getFoobar() {
      // Do something that returns some UTF-8 stuff (probably some XML).
    }
}

It seems that it always returns content encoded in ISO-8859-1, even if I have a CharacterEncodingFilter defined first in the chain like:

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

I have debugged and printed the string output to the logs, and it goes to the logfile just fine in UTF-8. However, it ends out in the browser as ISO-8859-1.


Issue Links:

  • #11225 @ResponseBody overwrites ContentType
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Nov 24, 2009

Arjen Poutsma commented

Moved to SPR, as the issue is not related to SWS>

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Jan 4, 2010

William Ashley commented

The CharacterEncodingFilter is intended to set the encoding on requests, not to dictate the encoding for responses.

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/filter/CharacterEncodingFilter.html

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Jan 14, 2010

Arjen Poutsma commented

As William stated above, the CharacterEncodingFilter specifies an encoding for requests, not responses.

Note that you can specify the response content-type for the StringHttpMessageConverter as follows:

<bean class="org.springframework.http.converter.StringHttpMessageConverter">
  <property name="supportedMediaTypes" value="text/plain;charset=UTF-8"/>
</bean>
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Jan 15, 2010

Morten Egelund Rasmussen commented

Uh..... The above solution wasn't one that I knew in my sleep....

Writing that into the relevant documentation would definitely be a welcome hit.

Thank you for the job done anyway. :-)

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Jan 20, 2010

Keith Donald commented

Also note if you set the "forceEncoding" property on the CharacterEncodingFilter to "true", the encoding will be set on the response as well.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Feb 23, 2010

Matthijs Bierman commented

Would it be possible to configure the default charset through a constructor property? Adjustment to the class would be minor:

    public ConfigurableStringHttpMessageConverter() {
        this(DEFAULT_CHARSET);
    }

    public ConfigurableStringHttpMessageConverter(Charset defaultCharset) {
        super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);
        this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());
    }

An advantage is that you do not need to rely on the accept header sent by the client for your encoding, as you do with supportedMediaTypes. Some clients will not be able or want to specify the charset, but you will want to force it. Is there a specific reason it does not default to UTF-8?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Feb 23, 2010

Matthijs Bierman commented

ConfigurableStringHttpMessageConverter should be StringHttpMessageConverter, sorry. I copied the code from my class where I tested this. Works fine.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Nov 24, 2010

Batbayar Bazarragchaa commented

@Keith
forceEncoding=true not working for me.

Following method is working wor me:
http://forum.springsource.org/showthread.php?t=78843

BTW I'm using spring framework 3.0.5.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

spring-issuemaster commented Mar 10, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.