当前位置:网站首页>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
边栏推荐
- 九章云极DataCanvas公司获评36氪「最受投资人关注的硬核科技企业」
- AttributeError: module ‘torch._C‘ has no attribute ‘_cuda_setDevice‘
- Servicemesh mainly solves three pain points
- JS variable case
- JDBC link Oracle reference code
- sublime使用技巧
- 【736. Lisp 语法解析】
- 基于Bevy游戏引擎和FPGA的双人游戏
- A row of code r shows the table of Cox regression model
- Why do many people misunderstand technical debt
猜你喜欢
随机推荐
App embedded H5 --- iPhone soft keyboard blocks input text
ClickHouse(03)ClickHouse怎么安装和部署
[hand torn STL] list
最全常用高数公式
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
R language principal component PCA, factor analysis, clustering analysis of regional economy analysis of Chongqing Economic Indicators
R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标
Thread和Runnable创建线程的方式对比
【愚公系列】2022年7月 Go教学课程 005-变量
What work items do programmers hate most in their daily work?
Flex layout and usage
01机器学习相关规定
记录一次压测经验总结
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
Basic knowledge of road loss of 3GPP channel model
National meteorological data / rainfall distribution data / solar radiation data /npp net primary productivity data / vegetation coverage data
Factor analysis r practice (with R installation tutorial and code)
Using thread class and runnable interface to realize the difference between multithreading
JS 的 try catch finally 中 return 的执行顺序
Oracle - views and sequences