当前位置:网站首页>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
边栏推荐
- Factor analysis r practice (with R installation tutorial and code)
- 3GPP信道模型路损基础知识
- Weebly mobile website editor mobile browsing New Era
- Comparison between thread and runnable in creating threads
- What work items do programmers hate most in their daily work?
- Thread和Runnable创建线程的方式对比
- R descriptive statistics and hypothesis testing
- What is JVM? What are the purposes of JVM tuning?
- How does vscade use the built-in browser?
- 高数中值定理总结
猜你喜欢

Monitoring cannot be started after Oracle modifies the computer name

Vscode automatically adds a semicolon and jumps to the next line

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

Markdown编辑器

If you‘re running pod install manually, make sure flutter pub get is executed first.

How does vscade use the built-in browser?

Mysql database (basic)

Ansible中的inventory主机清单(预祝你我有数不尽的鲜花和浪漫)

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

为什么很多人对技术债务产生误解
随机推荐
Batch normalization (Standardization) processing
ClickHouse(03)ClickHouse怎么安装和部署
JDBC link Oracle reference code
动态生成表格
A simple and beautiful regression table is produced in one line of code~
Field data acquisition and edge calculation scheme of CNC machine tools
Markdown编辑器
2.证券投资基金的概述
Flask project uses flask socketio exception: typeerror: function() argument 1 must be code, not str
第一篇论文的写作流程
Liste des hôtes d'inventaire dans ansible (je vous souhaite des fleurs et de la romance sans fin)
JS 的 try catch finally 中 return 的执行顺序
Leetcode notes
A line of R code draws the population pyramid
2. Overview of securities investment funds
offer如何选择该考虑哪些因素
Comparison between thread and runnable in creating threads
【數模】Matlab allcycles()函數的源代碼(2021a之前版本沒有)
If you ask me about R code debugging, I will tell you head, STR, help
In depth analysis of kubebuilder