springMvc与fastJson 集成时日期格式的问题

 WriteDateUseDateFormat默认日期格式为yyyy-MM-dd HH:mm:ss

 


        
            
                
                    
                        application/json;charset=UTF-8
                    

                

            
 
            
           
                
                    
                        text/html;charset=UTF-8
                        application/json
                    

                

                
                    
                       Date的日期转换器
                      
                        WriteDateUseDateFormat
                    

                

            
 

        

   

 

 

 

 

 

修改 WriteDateUseDateFormat默认日期格式为yyyy-MM-dd


package cn.smbms.tools;
 
import java.io.IOException;
 
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
 
public class JsonHttpMessageConverter extends FastJsonHttpMessageConverter {
 
    @Override
    protected void writeInternal(Object obj, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {
        // TODO Auto-generated method stub
        JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
        JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);
        super.writeInternal(obj, outputMessage);
 
    }
 
}


然后,将springMVC.xml(具体文件名以项目而定) 的配置修改为如下, 引用重写了writeInternal()方法的类进行json序列化

 
        
            
                
                    
                        application/json;charset=UTF-8
                    

                

            

            
            
               
                   
                        text/html;charset=UTF-8
                        application/json
                   

               

               
                   
                    WriteDateUseDateFormat
                        WriteMapNullValue
                        QuoteFieldNames
                   

               

           

            
        

   

你可能感兴趣的:(springMvc与fastJson 集成时日期格式的问题)