简体   繁体   English

如何使用JPA和Springboot JSON返回子对象详细信息

[英]How to JSON returning child object details using JPA and Springboot

I'm using Springboot + JPA to do a restful backend, I have two entities and I need to get a JSON response with child object details like this: 我正在使用Springboot + JPA做一个宁静的后端,我有两个实体,我需要得到一个带有子对象详细信息的JSON响应,如下所示:

{
  "mensagens": [
    {
        "id": "2",
        "origem_id": "1",
        "destino_id": "2",
        "assunto": "hello",
        "corpo": "hi, how are you?",
        "origem": {
            "id": "1",
            "nome_completo": "Leonard",
            "apelido": "leo"
        },
        "destino": {
            "id": "2",
            "nome_completo": "Mark",
            "apelido": "mark"
        }
    }
  ]
}

Can anyone help me? 谁能帮我?

====================================================================================================================================================== ================================================== ================================================== ==================================================

My classes are below: 我的课程如下:

This is my Entity Contact: 这是我的实体联系人:

@Entity
@Table(schema="schema")
public class Contact {

@Id
@GeneratedValue
@Column(name = "contact_id")
private long id;

@Column(name = "nome_completo", nullable = true)
@JsonProperty(value = "nome_completo")
private String nomeCompleto;

@Column(name = "apelido", nullable = true)
private String apelido;

@OneToMany(mappedBy = "origemId", cascade = CascadeType.ALL)
private List<Message> msgOrigem;

@OneToMany(mappedBy = "destinoId", cascade = CascadeType.ALL)
private List<Message> msgDestino;

// getters and setters

This is my Entity Message 这是我的实体讯息

@Entity
@Table(name = "message", schema = "schema")
public class Message {
@Id
@GeneratedValue
@Column(name = "msg_id")
private long id;

@Column(name = "origem_id")
private long origemId;

@Column(name = "destino_id")
private long destinoId;

@Column(name = "assunto")
private String assunto;

@Column(name = "corpo")
private String corpo;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "contactId")
private Contact contact;

// getters and setters

My repository: 我的资料库:

@RestResource(exported = false)
public interface MessageRepository extends JpaRepository<Message, Long> {}

My Class Messages: 我的班级留言:

public class Messages {

    private List<Message> mensagens;

    public List<Message> getMensagens() {
        return mensagens;
    }

    public void setMensagens(List<Message> mensagens) {
        this.mensagens = mensagens;
    }   
}

my rest controller: 我的休息控制器:

@RestController 
@RequestMapping(path = "/sdm/mensageiro/mensagens") 
public class ListMessagesController { 
    @Autowired 
    private MessageRepository repository; 

    @GetMapping 
    public Messages findAllMessages() { 
          Messages c = new Messages(); 
          c.setMensagens(repository.findAll()); 
          return c; 
    } 
 }

This is the class that run the springboot applcation: 这是运行springboot应用程序的类:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

I use the postman to retrive from Mysql data and now my result JSON is like below: 我使用邮递员从Mysql数据中检索,现在我的结果JSON如下所示:

{
    "mensagens": [
        {
            "id": 2,
            "origemId": 1,
            "destinoId": 2,
            "assunto": "Hello",
            "corpo": "hi, how are you?"
        }
     ]
}

Should update fetch type to EAGER in order to when getting the list persiste, here is things that you need to update: 应该将获取类型更新为EAGER,以便持久保存列表,这是您需要更新的内容:

@OneToMany(mappedBy = "origemId", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private List<Message> msgOrigem;

@OneToMany(mappedBy = "destinoId", cascade = CascadeType.ALL, fetch=FetchType.EAGER)
private List<Message> msgDestino;

And ignore Contact in order not to display contact again because you will have a recursive error. 并忽略“联系人”以免再次显示联系人,因为您将遇到递归错误。

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "contactId")
@JsonIgnore
private Contact contact;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM