做SpringMVC的JSON想使用FastJSON 按照网上的配置老是出现406错误!
配置如下
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 自动扫描的包名 -->
<context:component-scan base-package="/" />
<!-- 视图解释类 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=utf-8</value>
</list>
</property>
</bean>
<!-- <bean id="jsonConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> -->
<bean id="jsonConverter" class="cn.spingmvc.json.FastJsonMappingHttpMessageConverter">
<!-- <property name="supportedMediaTypes">
<list>
<value>text/plain;charset=utf-8</value>
<value>text/html;charset=utf-8</value>
<value>text/json;charset=utf-8</value>
<value>application/json;charset=utf-8</value>
</list>
</property> -->
<property name="serializerFeature">
<array>
<value>WriteMapNullValue</value>
<value>QuoteFieldNames</value>
</array>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverter"/>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<!-- 对静态资源文件的访问 方案一 (二选一) -->
<mvc:default-servlet-handler/>
<!-- 对静态资源文件的访问 方案二 (二选一)-->
<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>
<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>
</beans>
其中网上的都是使用自己写的转换器
package cn.spingmvc.json;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
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.GenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
public class FastJsonMappingHttpMessageConverter extends AbstractHttpMessageConverter<Object>
implements GenericHttpMessageConverter<Object> {
private static final Charset DEFAULT_CHARSET=Charset.forName("UTF-8");
//fastJson特性参数
private SerializerFeature[] serializerFeature;
public SerializerFeature[] getSerializerFeature() {
return serializerFeature;
}
public void setSerializerFeature(SerializerFeature[] serializerFeature) {
this.serializerFeature = serializerFeature;
}
public FastJsonMappingHttpMessageConverter(){
super(new MediaType("application", "json", DEFAULT_CHARSET));
}
@Override
public boolean canRead(Type paramType, Class<?> paramClass,
MediaType paramMediaType) {
return true;
}
@Override
public Object read(Type paramType, Class<?> paramClass,
HttpInputMessage paramHttpInputMessage) throws IOException,
HttpMessageNotReadableException {
return true;
}
@Override
protected boolean supports(Class<?> paramClass) {
return true;
}
@Override
protected Object readInternal(Class<? extends Object> paramClass,
HttpInputMessage paramHttpInputMessage) throws IOException,
HttpMessageNotReadableException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
while ((i = paramHttpInputMessage.getBody().read()) != -1) {
baos.write(i);
}
return JSON.parseArray(baos.toString(), paramClass);
}
@Override
protected void writeInternal(Object paramT,
HttpOutputMessage paramHttpOutputMessage) throws IOException,
HttpMessageNotWritableException {
String jsonString = JSON.toJSONString(paramT, serializerFeature);
OutputStream out = paramHttpOutputMessage.getBody();
out.write(jsonString.getBytes(DEFAULT_CHARSET));
out.flush();
out.close();
}
}
然后发现怎么也不行 其实fastJSON自己提供了转换器com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter 对比发现
@Override
protected boolean supports(Class<?> paramClass) {
return true;
}
这个官方的类是返回true 而我的返回是false修改后正常 调试代码发现这个是支持那些类进行转换的 如果设为false 所有的对象都不能转换成JSON