关于@reponseBody和response.getWriter().println()输出JSON的一个区别

先来看看response.getWriter().println()的使用及返回的结果:

 @RequestMapping({"/wxLogin"})
 public void wxLogin(HttpServletRequest request, HttpServletResponse response)throws 
  Exception
	  {
	    response.setCharacterEncoding("utf-8");
	    String json = "";
	    Gson gson = new Gson();
	    Map<String,Object> map = new HashMap<String,Object>();
	    PrintWriter out = response.getWriter();
        String id="sn258369";
        WXInfo wxInfo=wxInfoService.findById(id);
        String id2="sn357159";
        User user=userService.findById(id2);
        map.put("wxInfo", wxInfo);
        map.put("user",user);
        json=gson.toJson(map);
        out.print(json);
        out.flush();
        out.close();
     }

返回结果如下:

{"wxInfo":{"id":"189e725696fb44cd8f8c1519deb40148","nickName":"@\"nickname\" : @\"你你若安好111\"\"","sex":"1","country":"China","province":"Guangdong","city":"Shenzhen","unionId":"oWuV20tjS-8h12xxxc","openId":"oca0s6LxxxAU","avatarUrl":"http://thirdwx.qeUribdzW3GSQj2Q/132","mobilePhone":"139xxxxx802","subscribe":0,"createdDate":"2019-09-11 14:09:11.0","updatedDate":"2019-12-24 11:11:57"},"user":{"userId":"727","userName":"139xxxxx802","name":"Daniel","birthday":"1990-01-14 17:18:16.0","sex":"1","mobilePhone":"139xxxxx802","registerTime":"2019-06-05 18:02:11.0"},"status":2}

可以看到各个属性均有值,没有出现值为null的属性。再来看看使用@Responsebody的写法和返回结果:

    @RequestMapping("/wxLogin")
	@ResponseBody
	public Map<String, Object> wxLogin(HttpServletRequest request,HttpServletResponse response)throws Exception{
		response.setCharacterEncoding("utf-8");
		int status=0;
		Gson gson=new Gson();
		Map<String, Object>map=new HashMap<String, Object>();
        String id="sn258369";
        WXInfo wxInfo=wxInfoService.findById(id);
        String id2="sn357159";
        User user=userService.findById(id2);
        map.put("wxInfo", wxInfo);
        map.put("user",user);
        return map;
   }

返回结果如下:

{

    "wxInfo": {

        "id": "189e7256deb40148",

        "nickName": "@\"nickname\" : @\"你你若安好111安\"\"",

        "sex": "1",

        "country": "China",

        "province": "Guangdong",

        "city": "Shenzhen",

        "userId": null,

        "unionId": "oWuV20tjxxxDxc",

        "openId": "oca0s6xxxxeIH3AU",

        "avatarUrl": "http://thirdwx.qlogo.cn/2Q/132",

        "mobilePhone": "139xxxxx802",

        "subscribe": 0,

        "createdDate": "2019-09-11 14:09:11",

        "updatedDate": "2019-12-24 13:22:23"

    },

    "user": {

        "userId": "727",

        "userName": "139xxxxx802",

        "password": "",

        "name": "Daniel",

        "birthday": "1990-01-14",

        "sex": "1",

        "bloodType": null,

        "email": "",

        "height": null,

        "weight": null,

        "qqNum": null,

        "weChat": null,

        "weiBo": null,

        "homePhone": null,

        "mobilePhone": "1319xxxxx802",

        "officePhone": null,

        "country": null,

        "ctyId": null,

        "province": null,

        "city": null,

        "district": null,

        "address": null,

        "allergy": null,

        "remark": null,

        "dataURL": null,

        "registerTime": "2019-06-05 18:02:11.0",

        "ssuppliercode": null

    },

    "status": 2

}

可以看到,在返回user对象的时候,出现很多值为null的属性,这些属性均为user中的属性,但数据库查询到的值为null。

这是两种推送JSON方式,返回结果的差异,Gson已屏蔽掉值为null的属性,而@ResponseBody则根据对象的所有属性原原本本地返回。

发布了137 篇原创文章 · 获赞 28 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/shenxiaomo1688/article/details/103680552