JSON(二进制文件)
- 提问:为什么要使用JSON进行数据交互?
JSON在接口调用、HTML页面中较常用;
因为JSON格式较简单,解析较简单;
序列化:JSON -> JavaBean
反序列化:Java Bean -> JSON
SpringMVC的JSON交互
- 1.请求JSON,输出JSON:
客户端写JSON不方便,少用; - 2.请求Key/Value,输出JSON:常用
———————————————
- String 转成 JSONNode的方法
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
private static final ObjectMapper objectMappper = new ObjectMapper();
public static JsonNode strToJsonNode(String str){
try {
return objectMappper.readTree(str);
} catch (IOException e) {
return null;
}
}
———————————————
- 提问:@RequestBody和@RequestParam的区别?
@RequestParam
1.用来处理简单的参数绑定;
2.用来接收 Content-Type 是 application/x-www-form-urlencoded
(content-Type默认格式)编码的内容;
@RequestBody
1.处理非 application/x-www-form-urlencoded
编码的内容;
如:application/json
application/xml
;
另外:
GET请求中无HttpEntity,所以@RequestBody并不适用;
POST请求中,通过HttpEntity传递的参数,必须要在请求头中声明数据的类型Content-Type,SpringMVC通过使用HandlerAdapter 配置的HttpMessageConverters来解析HttpEntity中的数据,然后绑定到相应的bean上;
———————————————
环境配置:
———————————————
@RequestBody直接以JSONString接收前端JSON
@RestController
public class MyController {
@RequestMapping("/json")
public String haha(@RequestBody String jsonString){
System.out.println(jsonString);
return jsonString;
}
}
———————————————
@RequestBody以简单对象接收前端JSON
@RestController
public class MyController {
@RequestMapping("/json")
public String haha(@RequestBody User user){
System.out.println(user.toString());
return user.toString();
}
}
———————————————
@RequestBody以复杂对象接收前端JSON
@RestController
public class MyController {
@RequestMapping("/json")
public String haha(@RequestBody Team team){
System.out.println(team.toString());
return team.toString();
}
}
———————————————
@RequestBody与简单的@RequestParam()同时使用
@RestController
public class MyController {
@RequestMapping("/json")
public String haha(@RequestBody User user,@RequestParam("token") String token){
System.out.println(user.toString());
System.out.println(token);
return token + " -> " + user.toString();
}
}
———————————————
@RequestBody与复杂的@RequestParam()同时使用
———————————————
———————————————
———————————————
———————————————
———————————————
————————————————————————————-
FASTJSON
fastjson:将json字符串转化成map的五种方法
https://www.cnblogs.com/heqiyoujing/p/9432153.html
乱序问题
Entity属性前加上:@JSONField(ordinal = 数字)
——————————————————————————————
https://blog.csdn.net/srj1095530512/article/details/82529759
Java Bean:Student(name="bob",age=24)
JSON String:{"name":"bob","age":24}
JSON Object:{"name":"bob","age":24}
记忆
JSONString是很忠实的类型:
他转成什么都是:JSON.parseObject()
;什么转成他都是:JSON.toJSONString()
;
- 提问:Json字符串和Json对象有什么区别?
对象的属性可以用:对象.属性
进行调用;
而字符串就不能通过属性调用;
且:@RequestBody
接收到的是一个Json字符串;
——————————————————————————————
JSON.toJSONString()
Java Bean 转 JSON String
JSON.parseObject()
JSON String 转 Java Bean
JSON.toJSON()
Java Bean 转 JSON String
{"name":"bob","age":24}
JSON.toJavaObject()
JSON String 转 Java Bean
Student(name=bob, age=24)
JSON.toJSONString()
JSONObject 转 JSONString
JSON.parseObject()
JSONString 转 JSONObject
———————————————
把输入流转成JavaBean,且配置字符串(默认字符串):
目标对象 实例 = JSON.parseObject(
InputStream,
Charset.defaultCharset(),
目标对象.class
);
———————————————
———————————————
———————————————
———————————————
————————————————————————————-