【问题标题】:How to create modal class for give Json如何为给 Json 创建模态类
【发布时间】:2020-04-27 00:03:40
【问题描述】:

我在 Spring Boot 中使用 JPA 作为 ORM,我需要在 Spring Boot 中创建模态类。如何创建?

{
   "createdBy":2,
   "hospitalId":33,
   "doctorId":45,
   "advanceBookingDays":5,
   "dutyDetails":[
      {
         "tokenLimit":66,
         "date":"2010-4-6",
         "timeSlot":[
            {
               "startTime":"7:23:00",
               "endTime":"8:22:00"
            },
            {
               "startTime":"9:00:40",
               "endTime":"10:33:56"
            }
         ]
      },
      {
         "tokenLimit":32,
         "date":"2010-4-6",
         "advanceBookingDays":5,
         "timeSlot":[
            {
               "startTime":"7:23:00",
               "endTime":"8:22:00"
            },
            {
               "startTime":"9:00:40",
               "endTime":"10:33:56"
            }
         ]
      }
   ]
}

如何创建模态类

【问题讨论】:

  • 你的意思是要把这个json转换成你的实体类吗?

标签: java json spring spring-boot jpa


【解决方案1】:

如果您需要创建将成为 JPA 实体的模型,您可以查看 Spring Data JPA 指南:https://spring.io/guides/gs/accessing-data-jpa/#initial

(定义一个简单实体段落)

请记住,JPA 是 Java Persistence API,您将在每个示例中通过休眠使用它的实现。如果只使用 API 中定义的方法,您可以谈论纯 JPA。

【讨论】:

    【解决方案2】:

    这个工具可以帮到你。

    www.jsonschema2pojo.org

    https://github.com/joelittlejohn/jsonschema2pojo

    生成的代码如下:

    public class DutyDetail {
    
    public Integer tokenLimit;
    public String date;
    public List<TimeSlot> timeSlot = null;
    public Integer advanceBookingDays;
    
    }
    -----------------------------------com.example.Example.java---------------------
    
    package com.example;
    
    import java.util.List;
    
    public class Example {
    
    public Integer createdBy;
    public Integer hospitalId;
    public Integer doctorId;
    public Integer advanceBookingDays;
    public List<DutyDetail> dutyDetails = null;
    
    }
    -----------------------------------com.example.TimeSlot.java--------------------
    
    package com.example;
    
    
    public class TimeSlot {
    
    public String startTime;
    public String endTime;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 2021-11-22
      • 2021-02-17
      • 1970-01-01
      • 2016-01-21
      • 2019-12-19
      • 2020-06-26
      相关资源
      最近更新 更多