0

我正在使用带有fastxml jackson注释的spring boot v1.2.3,我试图将整个兄弟集合公开到单个JSON响应中,但似乎不能使用正确的注释和神秘来将添加的集合放入响应中代码。

有人可以帮我解决这个问题吗?

我不确定它是否是 Spring Boot 中的原因,或者只是注释之间的配置不正确。我想将集合从子数据库添加到 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"
        }
    }
}

但是当我调用兄弟集合的 URL 时,我得到了兄弟集合,只是我想要上面响应中的兄弟集合。

{
    "_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"
                    }
                }
            }
        ]
    }
}
4

2 回答 2

1

@JsonView(class)您可以在控制器方法上使用和使用相同类的相同注释来注释您的字段或吸气剂- 这是更好的解决方案,或者只是注释不期望的字段@JsonIgnore

是有关杰克逊配置的更多信息

于 2015-07-03T18:33:23.770 回答
1

kxyz 的回答给了我解决方案,但我想为其他人提供更明确的最终解决方案细节。

我没有使用 @JsonManagedReference 和 @JsonBackReference 注释。

我必须创建以下视图:

public class StationView {

    public interface Summary{}
    public interface SummaryWithSiblings extends Summary{}

}

和控制器(而不是使用启动时必须生成的 spring boot 神秘控制器),因此可以使用这个新的 StationView 对其进行注释

@RestController
public class StationController {

    @Autowired
    private StationService stationService;

    @JsonView(StationView.SummaryWithSiblings.class)
    @RequestMapping("/stations")
    public List<Station> getStations() {
        Integer clientId = 1234;
        return stationService.findByClientId(clientId);
    }

}

我在父类和兄弟类上注释了每个由 JSON 公开的类变量。

@JsonView(StationView.Summary.class)
@Column(name = "station_name")
private String stationName;)

和兄弟集合以同样的方式。

@JsonView(StationView.SummaryWithSiblings.class)
@OneToMany(mappedBy = "station", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private Set<StationTank> stationTanks;

我从 JSON 响应中得到类似的东西。

[
    {
        "stationId": 1,
        "stationName": "station name 1",
        "active": true,
        "stationTanks":
        [
            {
                "stationTankId": 1,
                "active": true
            },
            {
                "stationTankId": 2,
                "active": true
            }
        ]
    }
]
于 2015-07-04T23:03:23.953 回答