Spring4 @ResponseBody使用StringHttpMessageConverter将默认字符集设置为utf8

时间:2014-12-22 14:53:38

标签: spring rest utf-8 spring-java-config

我正在尝试使用ajax和rest来返回纯文本消息。

这是我的控制器:

@RequestMapping(value="/test", method=RequestMethod.PUT)
@ResponseBody
public String ajaxTest() {
    return "Characters test: ęółąśżźćń";
}

但不幸的是我得到了:

  Characters test: ?�???????

我知道我必须使StringHttpMessageConverter默认使用UTF-8字符集。 即使我有一些xml配置 https://jira.spring.io/browse/SPR-9099

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

但我使用Java配置而不是xml conf。

这是我的初始化程序;

public class Initializer implements WebApplicationInitializer {


    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(WebAppConfig.class);
        ctx.register(ThymeleafConfig.class);
        servletContext.addListener(new ContextLoaderListener(ctx));

        ctx.setServletContext(servletContext);

        Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setAsyncSupported(true);     
        servlet.setLoadOnStartup(1);
        // Allow to use Put and Delete method for REST architecture
        registerCharachterEncodingFilter(servletContext);
        registerHiddenFieldFilter(servletContext);

    }


    private void registerCharachterEncodingFilter(ServletContext aContext) {
        CharacterEncodingFilter cef = new CharacterEncodingFilter();
        cef.setForceEncoding(true);
        cef.setEncoding("UTF-8");
        aContext.addFilter("charachterEncodingFilter", cef).addMappingForUrlPatterns(null ,true, "/*");
    }
    private void registerHiddenFieldFilter(ServletContext aContext) {
        aContext.addFilter("hiddenHttpMethodFilter", new HiddenHttpMethodFilter()).addMappingForUrlPatterns(null ,true, "/*"); 
    }
    private StringHttpMessageConverter stringConverter(ServletContext aContext) {
        StringHttpMessageConverter shmc = new StringHttpMessageConverter();
        shmc.setSupportedMediaTypes(Arrays.asList(new MediaType("text", "plain", Charset.forName("UTF-8"))));
        return shmc;
    }
}

但我可以让它发挥作用。请帮忙。

1 个答案:

答案 0 :(得分:1)

<强> EDITED

试试这个:

@RequestMapping(value = "/find", produces = {"application/json; charset=UTF-8"})
@ResponseBody
public String find() {

    return "áéíóúÁÉÍÓÚ";

}

并在web.xml中添加

<filter>
    <filter-name>encoding-filter</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>encoding-filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>