您的位置:首页 > 编程语言 > Java开发

SpringMVC ajax JSON 中文乱码问题

2013-11-02 12:22 399 查看
转载自:http://xyly624.blog.51cto.com/842520/893606

 http://blog.csdn.net/dyllove98/article/details/9631553

由于之前一直都是研发自己的MVC和动态表单架构,最近因为一个技术研究的项目,开始接触Spring MVC。各种标注带来了很好的开发感受,但是今天被AJAX的json返回折磨了好久。一直都是乱码。尝试了网上的各种方法,都不理想。

       最后通过研究StringHttpMessageConverter源代码发现,开发者很坑的使用了"ISO-8859-1"作为默认编码。这种西欧编码在如今的开源届也不多见,不知道是不是Spring一直忘记改了。
public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {

public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");

private Charset getContentTypeCharset(MediaType contentType) {
if (contentType != null && contentType.getCharSet() != null) {
return contentType.getCharSet();
}
else {
return DEFAULT_CHARSET;
}
}


   发现这段之后,就好处理了,由于DEFAULT_CHARSET无法通过IoC进行修改,最直接的方式就是完成自己的AbstractHttpMessageConverter,命名为:UTF8StringHttpMessageConverter。逻辑和StringHttpMessageConverter
一致,只需将DEFAULT_CHARSET修改为UTF-8即可。
spring-servlet 配置

<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.abc.spring.UTF8StringHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>


controller中

@RequestMapping(value = "/getWeather",method = {RequestMethod.POST,RequestMethod.GET},produces="text/plain;charset=UTF-8")
@ResponseBody


转换类:

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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: