GsonSpringMVC+Gson/JackSon总结

Posted 写Bug的渣渣高

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GsonSpringMVC+Gson/JackSon总结相关的知识,希望对你有一定的参考价值。

本文会说在SpringMVC中,使用Gson和Jackson的一些使用过程区别。以及JackSon在spingMVC中的自动解析功能

本文会用到
1.postman:测试接口
2.jackson:
3.gson:
jackson和gson依赖的导入

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>

本文会分别写出使用jackson和gson进行json数据处理的代码

传入int

1.使用jackson

    @ResponseBody
    @RequestMapping("/recieve/int")
    public Map<String, String> recieveInt(
            @RequestBody Integer intNumber
    )
        System.out.println("jackson自动解析int数据"+intNumber);
        Map<String ,String> map=new HashMap<>();
        map.put("status","success");
        return map;
    

### 2.Gson手动解析

    @ResponseBody
    @RequestMapping("/recieve/int2")
    public Map<String,String> recieveInt2(
            HttpServletRequest request
    )
        String json=parseJson(request);

        Gson gson=new Gson();
        System.out.println("Gson手动解析结果"+gson.fromJson(json, int.class));

        Map<String ,String> map=new HashMap<>();
        map.put("status","success");
        return map;
    

传入double

第一种

    @ResponseBody
    @RequestMapping("/recieve/double")
    public String revieveDouble(
           @RequestBody double doubleNumber
    )
        System.out.println("jackson自动解析double数据"+doubleNumber);
        Gson gson=new Gson();
        Map<String ,String> map=new HashMap<>();
        map.put("status","success");
        String json=gson.toJson(map);
        return json;
    

第二种

    @ResponseBody
    @RequestMapping("recieve/double2")
    public String revieveDouble2(
            HttpServletRequest request
    )
        String json=parseJson(request);
        Gson gson=new Gson();
        System.out.println("Gson解析double结果:"+gson.fromJson(json,double.class));

        Map<String ,String> map=new HashMap<>();
        map.put("status","success");
        String returnJson=gson.toJson(map);
        return returnJson;
    

传入String

第一种

    @ResponseBody
    @RequestMapping("/recieve/string")
    public String recieveString(
            @RequestBody String str
    )
        Gson gson=new Gson();
        System.out.println("jackson自动解析数据:"+str);

        Map<String ,String> map=new HashMap<>();
        map.put("status","success");
        String returnJson=gson.toJson(map);
        return returnJson;
    

第二种

    @ResponseBody
    @RequestMapping("/recieve/string2")
    public String recieveString2(
            HttpServletRequest request
    )
        String json=parseJson(request);
        Gson gson=new Gson();
        System.out.println("Gson手动解析数据:"+gson.fromJson(json,String.class));
        Map<String ,String> map=new HashMap<>();
        map.put("status","success");
        String returnJson=gson.toJson(map);
        return returnJson;
    

传入对象

第一种

    @ResponseBody
    @RequestMapping("/recieve/user")
    public String recieveUser(
            @RequestBody User user
    )
        System.out.println("jackson自动解析数据"+user);
        Map<String ,String> map=new HashMap<>();
        map.put("status","success");
        Gson gson=new Gson();
        String returnJson=gson.toJson(map);
        return returnJson;
    

第二种

    @ResponseBody
    @RequestMapping("/recieve/user2")
    public String recieveUser2(
            HttpServletRequest request
    )
        String json=parseJson(request);
        Gson gson=new Gson();
        System.out.println("Gson手动解析数据:"+gson.fromJson(json,User.class));

        Map<String ,String> map=new HashMap<>();
        map.put("status","success");
        String returnJson=gson.toJson(map);
        return returnJson;
    

传入List<>

第一种

    @ResponseBody
    @RequestMapping("/recieve/user/list")
    public String recieveUserList(
            @RequestBody List<User> userList
    )
        System.out.println("jackson自动解析数据");
        for (User user:userList) 
            System.out.println(user.toString());
        

        Gson gson=new Gson();
        Map<String ,String> map=new HashMap<>();
        map.put("status","success");
        String returnJson=gson.toJson(map);
        return returnJson;
    

第二种

    @ResponseBody
    @RequestMapping("/recieve/user/list2")
    public String recieveUserList(
            HttpServletRequest request
    )
        String json=parseJson(request);
        System.out.println("未解析json数据"+json);
        Gson gson=new Gson();
        Type userListType = new TypeToken<ArrayList<User>>().getType();
        List<User> userList=gson.fromJson(json, userListType);
        System.out.println("Gson手动解析数据为:");
        for (User user:userList) 
            System.out.println(user.toString());
        

        Map<String ,String> map=new HashMap<>();
        map.put("status","success");
        String returnJson=gson.toJson(map);
        return returnJson;
    

传入Map

第一种

    @RequestMapping("/recieve/map")
    public String recieveMap(
            Map<String , String> map
    )
        Gson gson=new Gson();
        System.out.println("jackson自动解析map,其user值为"+map.get("user"));
        System.out.println("jackson自动解析map,其password值为"+map.get("password"));

        Map<String ,String> map1=new HashMap<>();
        map1.put("status","success");
        String returnJson=gson.toJson(map1);
        return returnJson;
    

第二种

    @ResponseBody
    @RequestMapping("/recieve/complex/map1")
    public String recieve1Map2(
            HttpServletRequest request
    )
        Gson gson=new Gson();
        String json=parseJson(request);
        Type mapType = new TypeToken<Map<String,String>>().getType();
        Map<String , String> map=gson.fromJson(json,mapType);
        System.out.println("Gson手动解析map,其user值为"+map.get("user"));
        System.out.println("Gson手动解析map,其password值为"+map.get("password"));

        Map<String ,String> map1=new HashMap<>();
        map1.put("status","success");
        String returnJson=gson.toJson(map1);
        return returnJson;
    

传入复杂/自定义对象

自定义对象
如果想使用毕竟复杂的对象,比如
user:存储用户信息
dataList:存放很多种类的List<>数据,
我们可以把这两个对象封装在同一个类中,比如封装成Data类
在初始化数据之后,直接将其gson.toJson即可;
在接收的时候,也只需要gons.fromJson(json,Data.class);

第一种

    @ResponseBody
    @RequestMapping("/recieve/complex/class")
    public String recieveComplexClass(
            Zoo zoo
    )
        System.out.println("jackson自动解析====>>>>>>");
        System.out.println(zoo.toString());
        System.out.println("观察其list是否解析成功");
        for (Lion l :zoo.getLionList()) 
            System.out.println(l.toString());
        
        for (Monkey m :zoo.getMonkeyList()) 
            System.out.println(m.toString());
        

        Gson gson=new Gson();
        Map<String ,String> map1=new HashMap<>();
        map1.put("status","success");
        String returnJson=gson.toJson(map1);
        return returnJson;
    

第二种

    @ResponseBody
    @RequestMapping("/recieve/complex/class2")
    public String recieveComplexClass2(
            HttpServletRequest request
    )
        Gson gson=new Gson();
        String json=parseJson(request);
        Zoo zoo=gson.fromJson(json,Zoo.class);
        System.out.println("获取到的zoo:"+zoo.toString());

        Map<String ,String> map1=new HashMap<>();
        map1.put("status","success");
        String returnJson=gson.toJson(map1);
        return returnJson;
    

以上是关于GsonSpringMVC+Gson/JackSon总结的主要内容,如果未能解决你的问题,请参考以下文章