当前位置:网站首页>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
边栏推荐
- If you ask me about R code debugging, I will tell you head, STR, help
- 记录一次压测经验总结
- 想要选择一些部门优先使用 OKR, 应该如何选择试点部门?
- 5G VoNR+之IMS Data Channel概念
- 基于Bevy游戏引擎和FPGA的双人游戏
- Code source de la fonction [analogique numérique] MATLAB allcycles () (non disponible avant 2021a)
- 【736. Lisp 语法解析】
- Leetcode minimum difference in student scores
- [Yugong series] go teaching course 005 variables in July 2022
- 窗口可不是什么便宜的东西
猜你喜欢

Meow, come, come: do you really know if, if else

3GPP信道模型路损基础知识

Why do many people misunderstand technical debt

R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标

记录一次压测经验总结

Monitoring cannot be started after Oracle modifies the computer name

基于Bevy游戏引擎和FPGA的双人游戏
![[Yugong series] go teaching course 005 variables in July 2022](/img/29/2bb30443e1e418556b5e08932f75b4.png)
[Yugong series] go teaching course 005 variables in July 2022

Depth first traversal template principle of tree and graph

【二叉树】二叉树寻路
随机推荐
STM32F103实现IAP在线升级应用程序
窗口可不是什么便宜的东西
第一篇论文的写作流程
Read of shell internal value command
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
STM32F103 realize IAP online upgrade application
Analyse approfondie de kubebuilder
Talk about the importance of making it clear
Jetson nano configures pytorch deep learning environment / / to be improved
Oracle - views and sequences
LabVIEW在打开一个新的引用,提示内存已满
PLC Analog output analog output FB analog2nda (Mitsubishi FX3U)
Servicemesh mainly solves three pain points
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
JS variable case output user name
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
动态生成表格
offer如何选择该考虑哪些因素
[736. LISP syntax parsing]
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)