标签:
一、框架版本

1 2 3 4 5 6 7 | <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> |
2、实现AbstractHttpMessageConverter抽象类(未解决)
http://xyly624.blog.51cto.com/842520/896704
该方法解决了返回乱码问题,但是easyui无法显示数据(有数据但是显示为空,原因不明)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | public class MappingFastJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private SerializerFeature[] serializerFeature; public SerializerFeature[] getSerializerFeature() { return serializerFeature; } public void setSerializerFeature(SerializerFeature[] serializerFeature) { this.serializerFeature = serializerFeature; } public MappingFastJsonHttpMessageConverter() { super(new MediaType("application", "json", DEFAULT_CHARSET)); } @Override public boolean canRead(Class<?> clazz, MediaType mediaType) { return true; } @Override public boolean canWrite(Class<?> clazz, MediaType mediaType) { return true; } @Override protected boolean supports(Class<?> clazz) { throw new UnsupportedOperationException(); } @Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i; while ((i = inputMessage.getBody().read()) != -1) { baos.write(i); } return JSON.parseArray(baos.toString(), clazz); } @Override protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { String jsonString = JSON.toJSONString(o, serializerFeature); OutputStream out = outputMessage.getBody(); out.write(jsonString.getBytes(DEFAULT_CHARSET)); out.flush(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!-- fastjosn spring support --> <bean id="jsonConverter" class="com.alibaba.fastjson.spring.support.MappingFastJsonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json" /> <property name="serializerFeature"> <list> <value>WriteMapNullValue</value> <value>QuoteFieldNames</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> |
3、@RequestMapping添加produces = "text/html;charset=UTF-8",在Controller或Action添加均可(解决问题)


SpringMVC整合fastjson、easyui 乱码问题
标签:
原文地址:http://www.cnblogs.com/gossip/p/5379250.html