如何访问嵌套的JSON对象值为JPA实体类

发布于 2025-01-28 02:50:11 字数 1146 浏览 5 评论 0原文

我有这样的有效载荷

{
    "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不一样的天空 2025-02-04 02:50:11

在到达控制器之前,您应该修改请求主体。

“您必须自己考虑应用程序绩效因素
实施之前“

选项1。使用requestBodyAdvice。

选项2。使用SpringHandlerionterceptor。

选项3。使用AOP

选项4。使用HTTP过滤器。


下面的解决方案仅在使用单独的DTO类时起作用。

private Map<String, String> meta = new HashMap<>();

String userID = importTrans.getMeta().get("userId");

我希望上述信息已回答上述信息片段你的问题。

You should modify your request body before reaching the controller.

"You must consider the application performance factors on your own
before implementation"

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.

private Map<String, String> meta = new HashMap<>();

String userID = importTrans.getMeta().get("userId");

I hope the above pieces of information answered your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文