当前位置:网站首页>Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
Why JSON is used for calls between interfaces, how fastjson is assigned, fastjson 1.2 [email protected] Mapping relatio
2022-07-07 05:02:00 【Hao Kai】
Why should calls between interfaces use json?
Interaction between applications , Unlike the interaction between front and back , Then it is convenient to define and name consistently , Because in normal development , It may be an interface written by multiple teams to access each other , Therefore, the parameter names passed by the receiver and the sender in the development process may not be exactly the same , therefore , You simply define an object to receive , If the names don't match , You will not get value .
If this situation is found during joint commissioning , Then change the field , Dependencies that may involve multiple files , It's a lot of trouble .
Therefore, see interactive use json To receive data .
fastjson How do you assign values ?
adopt @JSONField, The fields of the interface message can be mapped to @JSONField name Specified fields .
Then go through set Method to assign a value to your corresponding java bean attribute , So this is why you need to provide corresponding java bean Of set Method , And is public Why .
fastjson The latest version 1.2.83 Message and @JSONField Mapping relationship problem
1.2.83 Result table (1.2.70 All normal values )
message | @JSONField | Conversion result |
---|---|---|
“appId”:“app_id”, | @JSONField(name = “app_id”) | The value is null |
“app_id2”:“app_id2” | @JSONField(name = “appId2”) | Normal value |
“op_id”:“op_id”, | //bean Relationship ①: Hump reception @JSONField(name = “op_id”) private String opId; | Normal value |
“op_id1”:“op_id1”, | //bean Relationship ②: Original value receiving @JSONField(name = “op_id1”) private String op_id1; | Normal value |
“op_id2”:“op_id2”, | //bean Relationship ③: It is worth receiving @JSONField(name = “op_id2”) private String param; | Normal value |
Json message
{
"op_id":"op_id",
"op_id1":"op_id1",
"op_id2":"op_id2",
"appId":"app_id",
"app_id2":"app_id2"
}
Bean
@Data
public class Json1 {
/** * json Message and @JSONField Agreement */
//bean Relationship ①: Hump reception
@JSONField(name = "op_id")
private String opId;
//bean Relationship ②: Original value receiving
@JSONField(name = "op_id1")
private String op_id1;
//bean Relationship ③: It is worth receiving
@JSONField(name = "op_id2")
private String param;
/** * json Message transmission @JSONField Hump switching */
@JSONField(name = "app_id")
private String app_id;
@JSONField(name = "appId2")
private String appId2;
}
Controller
package com.example.helloworld.controller;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello ==== " + 222;
}
@RequestMapping(value = "/json1", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Json1 json1(@RequestBody Json1 json) {
System.out.println(json);
return json;
}
/** * Why requestJson Pick up ? I don't want to be with you bane The attribute is called the same , I also don't want to change , What do I do ? * @param requestJson * @return */
@RequestMapping(value = "/json2", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Json1 json1(@RequestBody String requestJson) {
System.out.println("request json:" + requestJson);
Json1 json1 = JSON.parseObject(requestJson, Json1.class);
System.out.println(json1);
return json1;
}
}
Output results
1.2.70
request json:{
"op_id":"op_id",
"op_id1":"op_id1",
"op_id2":"op_id2",
"appId":"app_id",
"app_id2":"app_id2"
}
Json1(opId=op_id, op_id1=op_id1, param=op_id2, app_id=app_id, appId2=app_id2)
1.2.83
request json:{
"op_id":"op_id",
"op_id1":"op_id1",
"op_id2":"op_id2",
"appId":"app_id",
"app_id2":"app_id2"
}
Json1(opId=op_id, op_id1=op_id1, param=op_id2, app_id=null, appId2=app_id2)
postman Set transmission json test
Source code address :https://gitlab.com/publicstudy/helloworld
边栏推荐
- 最全常用高数公式
- JS 的 try catch finally 中 return 的执行顺序
- Read of shell internal value command
- Tiktok may launch an independent grass planting community platform: will it become the second little red book
- Ansible中的inventory主機清單(預祝你我有數不盡的鮮花和浪漫)
- Factor analysis r practice (with R installation tutorial and code)
- Introduction to namespace Basics
- ServiceMesh主要解决的三大痛点
- How to choose an offer and what factors should be considered
- Weebly移动端网站编辑器 手机浏览新时代
猜你喜欢
一个酷酷的“幽灵”控制台工具
sublime使用技巧
Chapter 9 Yunji datacanvas was rated as 36 krypton "the hard core technology enterprise most concerned by investors"
How does vscade use the built-in browser?
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
批量归一化(标准化)处理
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
动态生成表格
Techniques d'utilisation de sublime
Sublime tips
随机推荐
JS input and output
How to choose an offer and what factors should be considered
Thread和Runnable创建线程的方式对比
Run the command once per second in Bash- Run command every second in Bash?
AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
史上最全学习率调整策略lr_scheduler
Leetcode longest public prefix
Oracle -- 视图与序列
Why do many people misunderstand technical debt
Monitoring cannot be started after Oracle modifies the computer name
Weebly mobile website editor mobile browsing New Era
Leetcode minimum difference in student scores
Introduction to the PureMVC series
JS also exports Excel
九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
Common Oracle SQL statements
Techniques d'utilisation de sublime
Appium practice | make the test faster, more stable and more reliable (I): slice test
STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建
3GPP信道模型路损基础知识