【问题标题】:Adding collection to JSON response using Spring Boot and fasterxml jackson and @OneToMany annotation使用 Spring Boot 和 fastxml jackson 和 @OneToMany 注释将集合添加到 JSON 响应
【发布时间】:2015-07-03 15:09:20
【问题描述】:

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

【问题讨论】:

  • 有谁知道是什么生成了“_links”:部分?我想知道这是从spring boot还是fastxml jackson库生成的。

标签: java json spring spring-boot fasterxml


【解决方案1】:

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

Here是关于jackson配置的更多信息

【讨论】:

  • 感谢您的回复。我注意到了这种实现,但不确定。会试一试的。再次感谢。
【解决方案2】:

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
            }
        ]
    }
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多