zoukankan      html  css  js  c++  java
  • Spring @ResponseBody 直接返回String乱码处理

    @RequestMapping(value = "test", method = {
            RequestMethod.GET, RequestMethod.POST
        })
        @ResponseBody
        public String getMenuByParentId(Model model, String parentId) {
            List<Menu> list = menuService.getByParentId(Long.valueOf(parentId));
            StringBuffer sb = new StringBuffer();
            sb.append("中文");
            return sb.toString();
        }

     以上代码在请求时中文将会出现乱码,解决方法:

    在Spring配置文件中加上以下代码。注意加在<mvc:annotation-driven />之前

    <!-- Json返回 乱码处理 -->
        <bean
            class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <bean
                        class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
                    <bean
                        class="org.springframework.http.converter.StringHttpMessageConverter">
                        <property name="supportedMediaTypes">
                            <list>
                                <value>text/plain;charset=UTF-8</value>
                            </list>
                        </property>
                    </bean>
                    <bean
                        class="org.springframework.http.converter.ResourceHttpMessageConverter" />
                    <bean
                        class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
                    <bean
                        class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
                    <bean
                        class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
                </list>
            </property>
        </bean>
  • 相关阅读:
    MFC弹出模拟对话框
    ansible中的playbook详解
    django后台管理--添加自定义action
    django后台管理-ModelAdmin对象
    django表单的api
    puppet的配置
    git常用命令
    HAProxy的日志配置以及ACL规则实现负载均衡
    HAproxy的安装与配置讲解
    实时监测网络流量
  • 原文地址:https://www.cnblogs.com/wiker/p/3171358.html
Copyright © 2011-2022 走看看