热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Spring@ResponseBody直接返回String乱码处理

@RequestMapping(value"test",method{RequestMethod.GET,RequestMethod.POST


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

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

在Spring配置文件中加上以下代码。注意加在之前


<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-8value>
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>

 

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

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

在Spring配置文件中加上以下代码。注意加在之前


<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-8value>
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>

 


推荐阅读
author-avatar
甄之恋
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有