当前位置:网站首页>接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
接口间调用为什么要用json、fastjson怎么赋值的、fastjson [email protected]映射关系问题
2022-07-06 22:37:00 【郝开】
接口间调用为什么要用json?
应用之间交互,不像前后台之间交互,那么方便可以定义命名一致,因为正常开发中,可能是多个团队编写的接口去互相访问,因此开发过程接收方和发送方传递的参数命名可能不完全一致,所以,你单纯的定义一个对象去接收,如果命名不一致,你就会得不到值。
如果在联调时才发现这种情况,再去改字段,可能涉及多个文件的依赖关系,改起来很麻烦。
因此接口见交互使用json来接收数据。
fastjson怎么赋值的?
通过@JSONField,可以使接口报文的字段映射到@JSONField name指定的字段。
然后再去通过set方法去赋值给你对应的java bean属性,所以这也是为什么需要你提供对应java bean的set方法,且是public的原因。
fastjson最新版本1.2.83报文与@JSONField映射关系问题
1.2.83结果表格(1.2.70都正常取值)
报文 | @JSONField | 转换结果 |
---|---|---|
“appId”:“app_id”, | @JSONField(name = “app_id”) | 取值为null |
“app_id2”:“app_id2” | @JSONField(name = “appId2”) | 正常取值 |
“op_id”:“op_id”, | //bean关系①:驼峰接收 @JSONField(name = “op_id”) private String opId; | 正常取值 |
“op_id1”:“op_id1”, | //bean关系②:原值接收 @JSONField(name = “op_id1”) private String op_id1; | 正常取值 |
“op_id2”:“op_id2”, | //bean关系③:它值接收 @JSONField(name = “op_id2”) private String param; | 正常取值 |
Json报文
{
"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报文与@JSONField一致 */
//bean关系①:驼峰接收
@JSONField(name = "op_id")
private String opId;
//bean关系②:原值接收
@JSONField(name = "op_id1")
private String op_id1;
//bean关系③:它值接收
@JSONField(name = "op_id2")
private String param;
/** * json报文传与@JSONField的驼峰转换 */
@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;
}
/** * 为什么用requestJson接?我不想和你的bane属性叫的一样,一样了我也不想改,怎么办? * @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;
}
}
输出结果
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设置传输json测试
边栏推荐
- A detailed explanation of head pose estimation [collect good articles]
- Code source de la fonction [analogique numérique] MATLAB allcycles () (non disponible avant 2021a)
- 窗口可不是什么便宜的东西
- Introduction to namespace Basics
- How to package the parsed Excel data into objects and write this object set into the database?
- Two methods of chromosome coordinate sequencing
- Decorator basic learning 02
- JS variable case output user name
- sscanf,sscanf_ S and its related usage "suggested collection"
- 指针与数组在函数中输入实现逆序输出
猜你喜欢
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
MySQL forgot how to change the password
A line of R code draws the population pyramid
01 machine learning related regulations
R语言主成分pca、因子分析、聚类对地区经济研究分析重庆市经济指标
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
Markdown editor
3GPP信道模型路损基础知识
Operand of null-aware operation ‘!‘ has type ‘SchedulerBinding‘ which excludes null.
[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet
随机推荐
Poor math students who once dropped out of school won the fields award this year
Leetcode notes
Why do many people misunderstand technical debt
3GPP信道模型路损基础知识
【线段树实战】最近的请求次数 + 区域和检索 - 数组可修改+我的日程安排表Ⅰ/Ⅲ
JS variable case output user name
Flex layout and usage
Wechat can play the trumpet. Pinduoduo was found guilty of infringement. The shipment of byte VR equipment ranks second in the world. Today, more big news is here
【數模】Matlab allcycles()函數的源代碼(2021a之前版本沒有)
PLC模拟量输出 模拟量输出FB analog2NDA(三菱FX3U)
Talk about the importance of making it clear
Advertising attribution: how to measure the value of buying volume?
Thesis landing strategy | how to get started quickly in academic thesis writing
U++4 接口 学习笔记
What is JVM? What are the purposes of JVM tuning?
Lessons and thoughts of the first SQL injection
namespace基础介绍
How to open win11 remote desktop connection? Five methods of win11 Remote Desktop Connection
[practice leads to truth] is the introduction of import and require really the same as what is said on the Internet
【数模】Matlab allcycles()函数的源代码(2021a之前版本没有)