springMVC利用FastJson接口返回json数据配置

为什么要使用Fastjson

我们能用fastjson来作什么?
1、替换其他所有的json库,java世界里没有其他的json库能够和fastjson可相比了。
2、使用fastjson的序列化和反序列化替换java serialize,java serialize不单性能慢,而且体制大。
3、使用fastjson替换hessian,json协议和hessian协议大小差不多一样,而且fastjson性能优越,10倍于hessian
4、把fastjson用于memached缓存对象数据。

Jackson在处理对象之前的循环嵌套关系时不便。

什么是对象间的循环嵌套?比如A有一个List,B对象里又有一个A对象,当然返回A对象的Json字符串时,如果是
Jackson就会发生异常,因为Jackson天生不具备处理这种关系的能力,而Fastjson正好具备了这种能力
(另,如果你用的是 Jackson,可以使用相应的注解来支持对象间的循环嵌套,具体是什么注解忘了,你可以Google一下Jackson循环嵌套就有很多答案)。]

springMVC使用fastJson很简单:

第一种:在springMVC的配置文件中作如下配置:

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

别忘了添加Fastjson的包,如果使用Maven,版本可以切换至最新版本,可使用如下设置(强烈建议使用Maven

com.alibaba
fastjson
1.2.7

当属性值为空的时候,fastjson默认是不输出的,

Map < String , Object > jsonMap = new HashMap< String , Object>();
jsonMap.put("a",1);
jsonMap.put("b","");
jsonMap.put("c",null);
jsonMap.put("d","zhenghuasheng");

String str = JSONObject.toJSONString(jsonMap);

System.out.println(str);

//输出结果:{"a":1,"b":"",d:"zhenghuasheng"}

从输出结果可以看出,null对应的key已经被过滤掉;这明显不是我们想要的结果,这时我们就需要用到fastjson的SerializerFeature序列化属性

也就是这个方法:JSONObject.toJSONString(Object object, SerializerFeature... features)

Fastjson的SerializerFeature序列化属性:

  • QuoteFieldNames———-输出key时是否使用双引号,默认为true

  • WriteMapNullValue——–是否输出值为null的字段,默认为false

  • WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null

  • WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null

  • WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null

  • WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非null

         
         QuoteFieldNames
         
         
         
         WriteNullNumberAsZero
         
         WriteNullListAsEmpty
         
         WriteNullStringAsEmpty
         
         WriteNullBooleanAsFalse
         
         WriteNullStringAsEmpty
         
         
    
         
         WriteDateUseDateFormat
      
    

第二种直接使用

@RequestMapping(value="/page/query", method=RequestMethod.POST)
    @ResponseBody
    public String queryQua(HttpServletRequest request, HttpServletResponse response) throws Exception{
        SerializeConfig seriConf = new SerializeConfig();
        seriConf.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
        Map params =  Request2MapUtil.wrapMap(request) ;
        User user = LoginUserUtil.getCurrentUser(request, response);
        
        params.put("userName",user.getUsername());
        int page = params.get("page") == null ? 1 : Integer.parseInt(params.get("page").toString());
        int limit = params.get("limit") == null ? 10 : Integer.parseInt(params.get("limit").toString());
        PageInfo pageInfo = customerService.queryCustomerList(params, limit, page);
        PageResult result = new PageResult(PageResult.SUCCESS, pageInfo.getTotal(), pageInfo.getList());
        return JSON.toJSONString(result, seriConf,SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteMapNullValue);
    }

你可能感兴趣的:(springMVC利用FastJson接口返回json数据配置)