当前位置:网站首页>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
边栏推荐
- Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
- [Yugong series] go teaching course 005 variables in July 2022
- IMS data channel concept of 5g vonr+
- 3. Type of fund
- 3.基金的类型
- STM32 system timer flashing LED
- 批量归一化(标准化)处理
- Common Oracle SQL statements
- [digital analog] source code of MATLAB allcycles() function (not available before 2021a)
- LabVIEW在打开一个新的引用,提示内存已满
猜你喜欢
Ansible报错:“msg“: “Invalid/incorrect password: Permission denied, please try again.“
Monitoring cannot be started after Oracle modifies the computer name
Analyse approfondie de kubebuilder
System framework of PureMVC
Introduction to namespace Basics
Ansible overview and module explanation (you just passed today, but yesterday came to your face)
为什么很多人对技术债务产生误解
U++ 元数据说明符 学习笔记
Local tool [Navicat] connects to remote [MySQL] operation
九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
随机推荐
Leetcode notes
A simple and beautiful regression table is produced in one line of code~
Depth first traversal template principle of tree and graph
Tree map: tree view - draw covid-19 array diagram
【二叉树】二叉树寻路
Using thread class and runnable interface to realize the difference between multithreading
Factor analysis r practice (with R installation tutorial and code)
[line segment tree practice] recent requests + area and retrieval - array modifiable + my schedule I / III
What is Web3
U++ 游戏类 学习笔记
Organize five stages of actual attack and defense drill
Decorator basic learning 02
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
关于01背包个人的一些理解
Batch normalization (Standardization) processing
窗口可不是什么便宜的东西
Introduction to namespace Basics
【Android Kotlin协程】利用CoroutineContext实现网络请求失败后重试逻辑
STM32F103ZE+SHT30检测环境温度与湿度(IIC模拟时序)
Section 1: (3) logic chip process substrate selection