java - 使用@JsonAlias 使用@RequestBody 转换成Pojo
问题描述
我有如下给出的 JSON 模式,
{
"prty_id": "hgh",
"customer_id": "ghjghjghj"
}
我有下面给出的模型类
public class PARTY {
@JsonAlias({"prty_id", "prtyId"})
String prtyId;
@JsonAlias({"customer_id", "customerId"})
String customerId;
......
}
当我尝试@RequestBody
使用 PARTY 主体作为方法参数的注释反序列@RequestBody
化时,我得到 POJO 类字段的空值。
@PostMapping(value="/party/db",produces="application/json",consumes="application/json")
public Party controller(@RequestBody Party body)
//here in body parameter only i am getting null values
{
ObjectMapper obj = new ObjectMapper();
String json = obj.writeValueAsString(body);
System.out.println("===="+json);
PARTY p = obj.readValue(json, PARTY.class);
System.out.println("------"+p);
p.toString();
return p;
}
解决方案
您需要在课堂上编写吸气剂,以便杰克逊去串行化可以工作。在您的定义中也更好地使用 CamelCase:
public class Party {
@JsonAlias({"prty_id", "prtyId"})
String prtyId;
@JsonAlias({"customer_id", "customerId"})
String customerId;
public String getPrtyId() {
return prtyId;
}
public String getCustomerId() {
return customerId;
}
}
编辑:添加控制器:
@PostMapping("/test")
public Party test(@RequestBody Party body) {
return body;
}
推荐阅读
- c - 使用 sscanf 忽略部分字符串
- api - Anlytics API 仅返回最近两天的用户数
- react-native - 错误 React-native-screen StackViewCard.js
- dialogflow-es - 谷歌平台上带有对话流的自定义有效负载
- automation - 如何在 conf 文件中更新没有节标题的 Puppet ini_setting 或 ini_subsetting 资源?
- html - 扩展名为 .prd 的文件已下载为 HTML 文件
- excel - 一次搜索两个词
- reactjs - Froala 所见即所得编辑器未在 React 中显示所有工具栏按钮
- javascript - 简化 NodeJS MVC 中的代码
- python - 在python的for循环中使用列表索引有什么意义?