java - 在不知道属性名称和属性数量的情况下处理 json 对象
问题描述
我尝试使用以下示例处理请求:
"type" : "NEWS",
"content" : {
"title" : "Test Message",
"message" : "This is a message",
"buttonCaption" : "Click me"
}
或者可能:
"type" : "NEWS",
"content" : {
"title" : "Test Message",
"message" : "This is a message",
"buttonCaption" : "Click me",
"anotherField" : "values"
}
有时可能:
"type" : "NEWS",
"content" : {
"name" : "Test Message",
"anotherProperties" : "This is a message",
"ohMyGodAnotherFields" : "Click me"
}
所以我不能创建一个特定的对象。如何在 Spring 控制器中处理它?
解决方案
您可以JsonNode
在资源类中使用,例如:
public class Foo {
private String type;
private JsonNode content;
// ...
}
并@RequestBody
在您的控制器中接受它:
@PostMapping
public ResponseEntity<Foo> foo(@RequestBody Foo foo){
// do something with your foo...
}
您可以在此处阅读更多 aboot JsonNode 。
推荐阅读
- model - MVC 核心表单发布返回 Null
- angular - 如何在打字稿的http post响应中解开对象?
- ios - Xcode:CollectionViewCell 图像在点击时变得可见,它不应该
- python - 如何从 Pandas 数据框中删除记录但保留异常?
- python - 如何将日期转换为日期时间
- kubernetes - 具有 hostPath 卷的 Kubernetes 状态集
- android - Navigation Components popBackStack() 是否保留 Fragment 状态?
- c++ - C++ 编译器可以执行哪些 C 编译器无法执行的优化?
- python - $inc 运算符无法在我的烧瓶应用程序中使用 mongodb atlas
- javascript - 加载其他 JSON 后如何运行 JSON