学习笔记--SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
来源:互联网 发布:用友软件好用吗 编辑:程序博客网 时间:2024/04/30 15:24
多费周折最终还是找到解决方案,并亲测通过,故分享之。
简单的说就是新建个转换类再注入。就那么简单,这就是开源的好处啊!
配置:
- <mvc:annotation-driven>
- <mvc:message-converters register-defaults="true">
- <bean class="com.abc.spring.UTF8StringHttpMessageConverter"/>
- </mvc:message-converters>
- </mvc:annotation-driven>
- public class UTF8StringHttpMessageConverter extends
- AbstractHttpMessageConverter<String> {
- public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
- private final List<Charset> availableCharsets;
- public UTF8StringHttpMessageConverter() {
- this(DEFAULT_CHARSET);
- }
- public UTF8StringHttpMessageConverter(Charset defaultCharset) {
- super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);
- this.availableCharsets = new ArrayList<Charset>(Charset
- .availableCharsets().values());
- }
- @Override
- protected boolean supports(Class<?> clazz) {
- return String.class.equals(clazz);
- }
- @Override
- protected String readInternal(Class<? extends String> clazz,
- HttpInputMessage inputMessage) throws IOException,
- HttpMessageNotReadableException {
- MediaType contentType = inputMessage.getHeaders().getContentType();
- Charset charset = contentType.getCharSet() != null ? contentType
- .getCharSet() : DEFAULT_CHARSET;
- return FileCopyUtils.copyToString(new InputStreamReader(inputMessage
- .getBody(), charset));
- }
- @Override
- protected void writeInternal(String t, HttpOutputMessage outputMessage)
- throws IOException, HttpMessageNotWritableException {
- MediaType contentType = outputMessage.getHeaders().getContentType();
- Charset charset = contentType.getCharSet() != null ? contentType
- .getCharSet() : DEFAULT_CHARSET;
- FileCopyUtils.copy(t, new OutputStreamWriter(outputMessage.getBody(),
- charset));
- }
- protected List<Charset> getAcceptedCharsets() {
- return this.availableCharsets;
- }
- @Override
- protected Long getContentLength(String s, MediaType contentType) {
- if (contentType != null && contentType.getCharSet() != null) {
- Charset charset = contentType.getCharSet();
- try {
- return (long) s.getBytes(charset.name()).length;
- } catch (UnsupportedEncodingException ex) {
- throw new InternalError(ex.getMessage());
- }
- } else {
- return null;
- }
- }
- }
另外写好后可能出现一些问题,因为我解决这个问题的时候就出了下面一个问题:The prefix "mvc" for element "mvc:annotation-driven" is not bound
解决办法在xml的beans中添加
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
另外上面是spring版本需要和你包的版本一致 不然会报错。网上找了很多方法,这个是我研究后解决的方法,可以用的
- 学习笔记--SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- SpringMVC Ajax请求时返回json中文字符串的乱码问题的解决方案
- SpringMVC下读取json返回字符串的乱码问题
- 笔记:解决springMVC返回中文乱码问题
- springmvc学习笔记--json--返回json的日期格式问题
- springmvc学习笔记--json--返回json的日期格式问题
- springmvc学习笔记--json--返回json的日期格式问题
- springmvc中@responseBody返回Json格式中文乱码问题
- SpringMVC中@ResponseBody注解返回json中文乱码问题
- springMvc框架解决返回Json格式中文乱码问题
- JSON返回的数据中文乱码问题!!!
- springmvc中@responseBody 返回json格式以及中文乱码问题的解决
- 用springmvc作接口时返回json数据中文乱码
- springMVC返回json数据时,字符串中文乱码
- springmvc responseBody返回中文乱码解决方案
- bapi:Post with clearing (FB05) using internal posting interface
- 关于Unicode支持,wchar_t * 到 char *的转换
- 通过几个例子看sed的模式空间与保持空间
- 10款免费且开源的项目管理工具
- 关于Integer.parseInt
- 学习笔记--SpringMVC 3.1下返回json时中文显示乱码问题的解决方案
- eclipse server Runtime Environment 环境配置
- 纠结
- STL iterator的自己实现
- hdu4632 Palindrome subsequence
- 牙齿保健
- java基础加强01 枚举
- C++ 虚函数表解析
- ACE框架简介以及一个基于ACE的C/S服务程序实例