谁在 Spring MVC 中设置响应内容类型(@ResponseBody) [英] Who sets response content-type in Spring MVC (@ResponseBody)

查看:22
本文介绍了谁在 Spring MVC 中设置响应内容类型(@ResponseBody)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的注解驱动 Spring MVC Java Web 应用程序在 jetty Web 服务器上运行(目前在 maven jetty 插件中).

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

我正在尝试使用一种仅返回字符串帮助文本的控制器方法来支持 AJAX.资源采用 UTF-8 编码,字符串也是如此,但我来自服务器的响应带有

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 

即使我的浏览器发送

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

我以某种方式使用 spring 的默认配置

I'm using somehow default configuration of spring

我找到了将这个 bean 添加到配置中的提示,但我认为它没有被使用,因为它说它不支持编码,而是使用默认编码.

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;
}

推荐答案

简单声明StringHttpMessageConverter bean是不够的,需要注入AnnotationMethodHandlerAdapter:

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>

然而,使用这种方法你必须重新定义所有HttpMessageConverter,而且它也不适用于<mvc:annotation-driven/>.

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

所以,也许最方便但最丑陋的方法是使用 BeanPostProcessor 拦截 AnnotationMethodHandlerAdapter 的实例化:

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 " />

这篇关于谁在 Spring MVC 中设置响应内容类型(@ResponseBody)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆