SpringMVC4直接返回String乱码
2016-05-11 17:57
573 查看
过滤器我就不说了。
如果你是要返回一个String的话,比较蛋疼,因为很少直接返回String,所以我最近才发现这个问题,在你的Spring mvc的Controller上加上:
或者你可以做点更高级的事:
MyStringHttpMessageConverter.java:
然后在Spring mvc的配置文件上加上:
如果你是要返回一个String的话,比较蛋疼,因为很少直接返回String,所以我最近才发现这个问题,在你的Spring mvc的Controller上加上:
@RequestMapping(value = "test", produces = "application/json; charset=utf-8")
或者你可以做点更高级的事:
MyStringHttpMessageConverter.java:
package cn.dubby.what.util; import org.springframework.http.HttpInputMessage; import org.springframework.http.HttpOutputMessage; import org.springframework.http.MediaType; import org.springframework.http.converter.AbstractHttpMessageConverter; import org.springframework.util.StreamUtils; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; /** * Created by dubby on 16/5/11. */ public class MyStringHttpMessageConverter extends AbstractHttpMessageConverter<String> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private final Charset defaultCharset; private final List<Charset> availableCharsets; private boolean writeAcceptCharset = true; /** * A default constructor that uses {@code "ISO-8859-1"} as the default charset. * * @see #MyStringHttpMessageConverter(Charset) */ public MyStringHttpMessageConverter() { this(DEFAULT_CHARSET); } /** * A constructor accepting a default charset to use if the requested content * type does not specify one. */ public MyStringHttpMessageConverter(Charset defaultCharset) { super(new MediaType("text", "plain", defaultCharset), MediaType.ALL); this.defaultCharset = defaultCharset; this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values()); } /** * Indicates whether the {@code Accept-Charset} should be written to any outgoing request. * <p>Default is {@code true}. */ public void setWriteAcceptCharset(boolean writeAcceptCharset) { this.writeAcceptCharset = writeAcceptCharset; } @Override public boolean supports(Class<?> clazz) { return String.class.equals(clazz); } @Override protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException { Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType()); return StreamUtils.copyToString(inputMessage.getBody(), charset); } @Override protected Long getContentLength(String str, MediaType contentType) { Charset charset = getContentTypeCharset(contentType); try { return (long) str.getBytes(charset.name()).length; } catch (UnsupportedEncodingException ex) { // should not occur throw new IllegalStateException(ex); } } @Override protected void writeInternal(String str, HttpOutputMessage outputMessage) throws IOException { if (this.writeAcceptCharset) { outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); } Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType()); StreamUtils.copy(str, charset, outputMessage.getBody()); } /** * Return the list of supported {@link Charset}s. * <p>By default, returns {@link Charset#availableCharsets()}. * Can be overridden in subclasses. * * @return the list of accepted charsets */ protected List<Charset> getAcceptedCharsets() { return this.availableCharsets; } private Charset getContentTypeCharset(MediaType contentType) { if (contentType != null && contentType.getCharSet() != null) { return contentType.getCharSet(); } else { return this.defaultCharset; } } }
然后在Spring mvc的配置文件上加上:
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="cn.dubby.what.util.MyStringHttpMessageConverter"/> </mvc:annotation-driven>
相关文章推荐
- Myeclipse打不开,弹出对话框The Myeclipse executable launcher was unable to locate its companion shared library ”
- java中的匿名内部类总结
- myeclipse新建一个工作空间要做的事
- Java cookie的使用
- eclipse 3.7 没有jadClipse选项
- Spring Aop实例之xml配置
- spring 的优点?什么是DI机制? 什么是AOP?
- java中spring+springMVC+Mybatis主流框架maven的pom.xml配置参考
- 实例解析Java程序中正则表达式的贪婪模式匹配
- java 获取程序执行时间
- selenium java等待alert对话框弹出,并关闭对话框
- java枚举类型enum的使用
- struts2的result中传递多个参数
- java 匿名内部类
- decimalFormat("#","##0.00") java
- Spring基础知识(4)
- Java内存区域与内存溢出异常
- Hibeinate: Could not determine type for: java.util.List, at table: address_inf, for columns: [org.hi
- java内存溢出和内存泄露
- 【java】运行java程序