为了账号安全,请及时绑定邮箱和手机立即绑定

springmvc 解决@ResponseBody注解返回中文乱码

标签:
Java

使用messageConverters

复制代码

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >  
    <property name="messageConverters">  
        <list>  
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />  
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                <property name="supportedMediaTypes">  
                    <list>  
                        <value>text/plain;charset=utf-8</value>  
                        <value>text/html;charset=UTF-8</value>  
                    </list>  
                </property>  
            </bean>  
        </list>  
    </property>  </bean>

复制代码

在springmvc的配置文件中,将消息转换器的bean放在 <mvc:annotation-driven/> 的前面(上面),否则配置不会生效

 

需要jackson的依赖

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.8</version>
        </dependency>

如果上面这个不够,那就再追加或者替换为:

复制代码

<dependency>  
    <groupId>org.codehaus.jackson</groupId>  
    <artifactId>jackson-mapper-asl</artifactId>  
    <version>1.9.13</version>  </dependency>  <dependency>  
    <groupId>org.codehaus.jackson</groupId>  
    <artifactId>jackson-core-asl</artifactId>  
    <version>1.9.13</version>  </dependency>

复制代码

原文出处: https://www.cnblogs.com/kinome/p/9626375.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消