Search code examples
javaweb-applicationsspring-mvccharacter-encoding

Who sets response content-type in Spring MVC (@ResponseBody)


I'm having in my Annotation driven Spring MVC Java web application runned on jetty web server (currently in maven jetty plugin).

I'm trying to do some AJAX support with one controller method returning just String help text. Resources are in UTF-8 encoding and so is the string, but my response from server comes with

content-encoding: text/plain;charset=ISO-8859-1 

even when my browser sends

Accept-Charset  windows-1250,utf-8;q=0.7,*;q=0.7

I'm using somehow default configuration of spring

I have found a hint to add this bean to the configuration, but I think it's just not used, because it says it does not support the encoding and a default one is used instead.

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

My controller code is (note that this change of response type is not working for me):

@RequestMapping(value = "ajax/gethelp")
public @ResponseBody String handleGetHelp(Locale loc, String code, HttpServletResponse response) {
    log.debug("Getting help for code: " + code);
    response.setContentType("text/plain;charset=UTF-8");
    String help = messageSource.getMessage(code, null, loc);
    log.debug("Help is: " + help);
    return help;
}

Solution

  • Simple declaration of the StringHttpMessageConverter bean is not enough, you need to inject it into AnnotationMethodHandlerAdapter:

    <bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <array>
                <bean class = "org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
                </bean>
            </array>
        </property>
    </bean>
    

    However, using this method you have to redefine all HttpMessageConverters, and also it doesn't work with <mvc:annotation-driven />.

    So, perhaps the most convenient but ugly method is to intercept instantiation of the AnnotationMethodHandlerAdapter with BeanPostProcessor:

    public class EncodingPostProcessor implements BeanPostProcessor {
        public Object postProcessBeforeInitialization(Object bean, String name)
                throws BeansException {
            if (bean instanceof AnnotationMethodHandlerAdapter) {
                HttpMessageConverter<?>[] convs = ((AnnotationMethodHandlerAdapter) bean).getMessageConverters();
                for (HttpMessageConverter<?> conv: convs) {
                    if (conv instanceof StringHttpMessageConverter) {
                        ((StringHttpMessageConverter) conv).setSupportedMediaTypes(
                            Arrays.asList(new MediaType("text", "html", 
                                Charset.forName("UTF-8"))));
                    }
                }
            }
            return bean;
        }
    
        public Object postProcessAfterInitialization(Object bean, String name)
                throws BeansException {
            return bean;
        }
    }
    

    -

    <bean class = "EncodingPostProcessor " />