我使用springxml jackson annotations的spring引導v1.2.3,並試圖將整個兄弟集合公開到單個JSON響應中,但可以似乎只是使用正確的註釋和神祕代碼將新增的收藏集添加到響應中。使用Spring Boot和fasterxml jackson和@OneToMany註釋將集合添加到JSON響應
有人可以幫我對這個問題進行歧視嗎?
我不確定它是否是彈簧引導內的東西是註釋之間的原因或只是註釋配置。我想將這個集合添加到子數據庫的json中。
@Entity
@Table(name = "station")
public class Station implements Serializable {
@OneToMany(mappedBy = "station", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private Set<StationTank> stationTanks;
@JsonManagedReference("station-tank")
public Set<StationTank> getStationTanks() {
System.out.println("get tank count: " + stationTanks.size());
return stationTanks;
}
public void setStationTanks(Set<StationTank> stationTanks) {
System.out.println("set tank count: " + stationTanks.size());
this.stationTanks = stationTanks;
}
}
@Entity
@Table(name = "station_tank")
public class StationTank implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "station_tank_id")
private long stationTankId;
@Column(name = "active",nullable=true, columnDefinition="Boolean default false")
private Boolean active;
@ManyToOne(cascade = CascadeType.ALL, optional = false) // as defined in schema
@JoinColumn(name = "station_id")
private Station station;
@JsonBackReference("station-tank")
public Station getStation() {
return station;
}
public void setStation(Station station) {
this.station = station;
}
}
調用此URL我沒有將兄弟集合添加到響應中,我希望響應中的兄弟集合。 http://localhost:8080/stations/1
{
"stationId": 1,
"stationName": "station name 1",
"active": true,
"_links":
{
"self":
{
"href": "http://localhost:8080/stations/1"
},
"stationTanks":
{
"href": "http://localhost:8080/stations/1/stationTanks"
}
}
}
但是,當我呼籲兄弟收集我得到的兄弟收集精細的網址,只是我想在上面響應同級集合。
{
"_embedded":
{
"station_tanks":
[
{
"stationTankId": 1,
"active": true,
"_links":
{
"self":
{
"href": "http://localhost:8080/station_tanks/1"
},
"station":
{
"href": "http://localhost:8080/station_tanks/1/station"
}
}
},
{
"stationTankId": 2,
"active": true,
"_links":
{
"self":
{
"href": "http://localhost:8080/station_tanks/2"
},
"station":
{
"href": "http://localhost:8080/station_tanks/2/station"
}
}
}
]
}
}
有誰知道什麼產生「_links」:部分?我想知道這是從spring引導還是更快的xml jackson庫生成的。 –