Estou com uma aplicação Java usando Spring Boot para servir uma API REST (ou quase isso) com JSON.
Gostaria de configurar para trazer a entidade filha automaticamente.
@Entity
public class City {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(targetEntity=State.class, fetch = FetchType.EAGER)
@JoinColumn(name="state_id")
private State state;
// ...
Atualmente ele entrega assim:
"id" : 1,
"name" : "Cidade exemplo",
"capital" : true,
"_links" : {
"self" : {
"href" : "http://meu_dominio/rest/cities/1"
},
"city" : {
"href" : "http://meu_dominio/rest/cities/1"
},
"state" : {
"href" : "http://meu_dominio/rest/cities/1/state"
}
}
Porém gostaria que fosse assim:
"id" : 1,
"name" : "Cidade exemplo",
"capital" : true,
"state" : {
"id" : 1,
"name" : "Estado Exemplo",
"initials" : "EX",
"_links" : {
"self" : {
"href" : "http://meu_dominio/rest/states/1"
}
},
"_links" : {
"self" : {
"href" : "http://meu_dominio/rest/cities/1"
},
"city" : {
"href" : "http://meu_dominio/rest/cities/1"
},
"state" : {
"href" : "http://meu_dominio/rest/states/1"
}
}
Questão também postada no Stackoverflow, mas sem sucesso de resposta lá.