作者:小呀么小果冻 | 来源:互联网 | 2023-09-09 10:04
篇首语:本文由编程笔记#小编为大家整理,主要介绍了GsonSpringMVC+Gson/JackSon总结相关的知识,希望对你有一定的参考价值。本文会说在Spr
篇首语:本文由编程笔记#小编为大家整理,主要介绍了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
&#64;ResponseBody
&#64;RequestMapping("/recieve/int")
public Map<String, String> recieveInt(
&#64;RequestBody Integer intNumber
)
System.out.println("jackson自动解析int数据"&#43;intNumber);
Map<String ,String> map&#61;new HashMap<>();
map.put("status","success");
return map;
### 2.Gson手动解析
&#64;ResponseBody
&#64;RequestMapping("/recieve/int2")
public Map<String,String> recieveInt2(
HttpServletRequest request
)
String json&#61;parseJson(request);
Gson gson&#61;new Gson();
System.out.println("Gson手动解析结果"&#43;gson.fromJson(json, int.class));
Map<String ,String> map&#61;new HashMap<>();
map.put("status","success");
return map;
传入double
第一种
&#64;ResponseBody
&#64;RequestMapping("/recieve/double")
public String revieveDouble(
&#64;RequestBody double doubleNumber
)
System.out.println("jackson自动解析double数据"&#43;doubleNumber);
Gson gson&#61;new Gson();
Map<String ,String> map&#61;new HashMap<>();
map.put("status","success");
String json&#61;gson.toJson(map);
return json;
第二种
&#64;ResponseBody
&#64;RequestMapping("recieve/double2")
public String revieveDouble2(
HttpServletRequest request
)
String json&#61;parseJson(request);
Gson gson&#61;new Gson();
System.out.println("Gson解析double结果:"&#43;gson.fromJson(json,double.class));
Map<String ,String> map&#61;new HashMap<>();
map.put("status","success");
String returnJson&#61;gson.toJson(map);
return returnJson;
传入String
第一种
&#64;ResponseBody
&#64;RequestMapping("/recieve/string")
public String recieveString(
&#64;RequestBody String str
)
Gson gson&#61;new Gson();
System.out.println("jackson自动解析数据:"&#43;str);
Map<String ,String> map&#61;new HashMap<>();
map.put("status","success");
String returnJson&#61;gson.toJson(map);
return returnJson;
第二种
&#64;ResponseBody
&#64;RequestMapping("/recieve/string2")
public String recieveString2(
HttpServletRequest request
)
String json&#61;parseJson(request);
Gson gson&#61;new Gson();
System.out.println("Gson手动解析数据:"&#43;gson.fromJson(json,String.class));
Map<String ,String> map&#61;new HashMap<>();
map.put("status","success");
String returnJson&#61;gson.toJson(map);
return returnJson;
传入对象
第一种
&#64;ResponseBody
&#64;RequestMapping("/recieve/user")
public String recieveUser(
&#64;RequestBody User user
)
System.out.println("jackson自动解析数据"&#43;user);
Map<String ,String> map&#61;new HashMap<>();
map.put("status","success");
Gson gson&#61;new Gson();
String returnJson&#61;gson.toJson(map);
return returnJson;
第二种
&#64;ResponseBody
&#64;RequestMapping("/recieve/user2")
public String recieveUser2(
HttpServletRequest request
)
String json&#61;parseJson(request);
Gson gson&#61;new Gson();
System.out.println("Gson手动解析数据:"&#43;gson.fromJson(json,User.class));
Map<String ,String> map&#61;new HashMap<>();
map.put("status","success");
String returnJson&#61;gson.toJson(map);
return returnJson;
传入List<>
第一种
&#64;ResponseBody
&#64;RequestMapping("/recieve/user/list")
public String recieveUserList(
&#64;RequestBody List<User> userList
)
System.out.println("jackson自动解析数据");
for (User user:userList)
System.out.println(user.toString());
Gson gson&#61;new Gson();
Map<String ,String> map&#61;new HashMap<>();
map.put("status","success");
String returnJson&#61;gson.toJson(map);
return returnJson;
第二种
&#64;ResponseBody
&#64;RequestMapping("/recieve/user/list2")
public String recieveUserList(
HttpServletRequest request
)
String json&#61;parseJson(request);
System.out.println("未解析json数据"&#43;json);
Gson gson&#61;new Gson();
Type userListType &#61; new TypeToken<ArrayList<User>>().getType();
List<User> userList&#61;gson.fromJson(json, userListType);
System.out.println("Gson手动解析数据为:");
for (User user:userList)
System.out.println(user.toString());
Map<String ,String> map&#61;new HashMap<>();
map.put("status","success");
String returnJson&#61;gson.toJson(map);
return returnJson;
传入Map
第一种
&#64;RequestMapping("/recieve/map")
public String recieveMap(
Map<String , String> map
)
Gson gson&#61;new Gson();
System.out.println("jackson自动解析map,其user值为"&#43;map.get("user"));
System.out.println("jackson自动解析map,其password值为"&#43;map.get("password"));
Map<String ,String> map1&#61;new HashMap<>();
map1.put("status","success");
String returnJson&#61;gson.toJson(map1);
return returnJson;
第二种
&#64;ResponseBody
&#64;RequestMapping("/recieve/complex/map1")
public String recieve1Map2(
HttpServletRequest request
)
Gson gson&#61;new Gson();
String json&#61;parseJson(request);
Type mapType &#61; new TypeToken<Map<String,String>>().getType();
Map<String , String> map&#61;gson.fromJson(json,mapType);
System.out.println("Gson手动解析map,其user值为"&#43;map.get("user"));
System.out.println("Gson手动解析map,其password值为"&#43;map.get("password"));
Map<String ,String> map1&#61;new HashMap<>();
map1.put("status","success");
String returnJson&#61;gson.toJson(map1);
return returnJson;
传入复杂/自定义对象
自定义对象
如果想使用毕竟复杂的对象&#xff0c;比如
user:存储用户信息
dataList:存放很多种类的List<>数据&#xff0c;
我们可以把这两个对象封装在同一个类中&#xff0c;比如封装成Data类
在初始化数据之后&#xff0c;直接将其gson.toJson即可&#xff1b;
在接收的时候&#xff0c;也只需要gons.fromJson(json,Data.class);
第一种
&#64;ResponseBody
&#64;RequestMapping("/recieve/complex/class")
public String recieveComplexClass(
Zoo zoo
)
System.out.println("jackson自动解析&#61;&#61;&#61;&#61;>>>>>>");
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&#61;new Gson();
Map<String ,String> map1&#61;new HashMap<>();
map1.put("status","success");
String returnJson&#61;gson.toJson(map1);
return returnJson;
第二种
&#64;ResponseBody
&#64;RequestMapping("/recieve/complex/class2")
public String recieveComplexClass2(
HttpServletRequest request
)
Gson gson&#61;new Gson();
String json&#61;parseJson(request);
Zoo zoo&#61;gson.fromJson(json,Zoo.class);
System.out.println("获取到的zoo:"&#43;zoo.toString());
Map<String ,String> map1&#61;new HashMap<>();
map1.put("status","success");
String returnJson&#61;gson.toJson(map1);
return returnJson;