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

时间:2018-11-04 14:49:31

标签: java json rest spring-data-jpa backend

我正在使用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"
        }
    }
  ]
}

有人可以帮助我吗?

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

我的课在下面:

这是我的实体联系人:

@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

这是我的实体消息

@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

我的存储库:

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

我的班级留言:

public class Messages {

    private List<Message> mensagens;

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

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

我的休息控制器:

@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; 
    } 
 }

这是运行springboot应用程序的类:

@SpringBootApplication
public class Application {

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

我使用邮递员从Mysql数据中检索,现在我的结果JSON如下所示:

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

1 个答案:

答案 0 :(得分:0)

应该将获取类型更新为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;

并忽略联系人,以便不再显示联系人,因为您将遇到递归错误。

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