在spring的json中会出现乱码,是因为项目中使用utf-8,而annotation-driven 默认转码为ISO-8859-1重新定义为UTF-8,直接贴代码,记录下,
-
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
-
<property name="messageConverters">
-
<list>
-
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
-
<property name="supportedMediaTypes">
-
<list>
-
<value>text/html;charset=UTF-8</value>
-
<value>text/json;charset=UTF-8</value>
-
<value>application/json;charset=UTF-8</value>
-
</list>
-
</property>
-
</bean>
-
</list>
-
</property>
-
</bean>
-
<mvc:annotation-driven
-
content-negotiation-manager="contentNegotiationManager" >
-
<mvc:message-converters register-defaults="true">
-
<bean class="com.kuanrf.web.core.filter.UTF8StringHttpMessageConverter"/>
-
</mvc:message-converters>
-
</mvc:annotation-driven>
-
<bean id="contentNegotiationManager"
-
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
-
<property name="mediaTypes">
-
<value>
-
html=text/html
-
json=application/json
-
</value>
-
</property>
-
<property name="defaultContentType" value="text/html" />
-
</bean>
-
package com.kuanrf.web.core.filter;
-
-
import java.io.IOException;
-
import java.io.InputStreamReader;
-
import java.io.OutputStreamWriter;
-
import java.io.UnsupportedEncodingException;
-
import java.nio.charset.Charset;
-
import java.util.ArrayList;
-
import java.util.List;
-
-
import org.springframework.http.HttpInputMessage;
-
import org.springframework.http.HttpOutputMessage;
-
import org.springframework.http.MediaType;
-
import org.springframework.http.converter.AbstractHttpMessageConverter;
-
import org.springframework.http.converter.HttpMessageNotReadableException;
-
import org.springframework.http.converter.HttpMessageNotWritableException;
-
import org.springframework.util.FileCopyUtils;
-
-
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;
-
}
-
}
-
}
阅读(1455) | 评论(0) | 转发(0) |