繁体   English   中英

使用Spring Boot,fasterxml jackson和@OneToMany注释将集合添加到JSON响应中

[英]Adding collection to JSON response using Spring Boot and fasterxml jackson and @OneToMany annotation

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

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

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

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

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

我必须创建以下视图:

public class StationView {

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

}

和控制器(而不是使用必须在启动时生成的spring boot mystery控制器),因此可以使用此新的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
            }
        ]
    }
]

暂无
暂无

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

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