SpringMVC 使用FastJSON 406錯誤解決

做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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章