如何访问嵌套的JSON对象值为JPA实体类
我有这样的有效载荷
{
"eventId":"ep9_0579af51",
"eventTime":"5/11/2022 5:50:58 PM",
"eventType":"UpdateTransaction",
"meta":{
"userId":"vkp",
"resourceType":"Transaction/DataDocs"
}
}
,需要将此JSON字段映射到单个实体类中。
@PostMapping(path = "/id", consumes = "application/json")
public ResponseEntity<ImportTrans> import(@RequestBody ImportTrans importTrans) {
return ResponseEntity.of(Optional.ofNullable(repository.save(importTrans););
}
@Table(name = "IMPORT_TRANS")
@Entity
public class ImportTrans implements Serializable {
@Id
private Long processId;// AutoGenerator
private String eventId;
private Date eventTime;
private String eventType;
private String userId; // I dont want create new class for meta . Is there any way i
//can access meta.userId in ImportTrans class.
private String resourceType;
}
如何从META
中访问importtrans
的数据,而无需为其创建单独的类?
I have a payload like this
{
"eventId":"ep9_0579af51",
"eventTime":"5/11/2022 5:50:58 PM",
"eventType":"UpdateTransaction",
"meta":{
"userId":"vkp",
"resourceType":"Transaction/DataDocs"
}
}
I need to map this json fields into a single entity class .
@PostMapping(path = "/id", consumes = "application/json")
public ResponseEntity<ImportTrans> import(@RequestBody ImportTrans importTrans) {
return ResponseEntity.of(Optional.ofNullable(repository.save(importTrans););
}
@Table(name = "IMPORT_TRANS")
@Entity
public class ImportTrans implements Serializable {
@Id
private Long processId;// AutoGenerator
private String eventId;
private Date eventTime;
private String eventType;
private String userId; // I dont want create new class for meta . Is there any way i
//can access meta.userId in ImportTrans class.
private String resourceType;
}
How can I access data from meta
from ImportTrans
without creating a separate class for it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在到达控制器之前,您应该修改请求主体。
选项1。使用requestBodyAdvice。
选项2。使用SpringHandlerionterceptor。
选项3。使用AOP
选项4。使用HTTP过滤器。
下面的解决方案仅在使用单独的DTO类时起作用。
我希望上述信息已回答上述信息片段你的问题。
You should modify your request body before reaching the controller.
Option 1. Using RequestBodyAdvice.
Option 2. Using Spring HandlerInterceptor.
Option 3. Use AOP
Option 4. Using HTTP Filter.
The below solution only works if you are using a separate DTO class.
I hope the above pieces of information answered your question.