Search code examples
javajsonspringspring-bootfasterxml

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


I'm using spring boot v1.2.3 with fasterxml jackson annotations and I'm trying to expose the entire sibling collection into the single JSON response, but can't just seem to get the added collection into the response using the right annotations and mystery code.

Can someone help me dymistify the issue?

I'm not sure if it's something within spring boot is the cause or just incorret configuration between annotations. I'd like to have the collection added to the json from the child database.

@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;
    }

}

Calling this URL I don't have the sibling collection added to the response and I want the sibling collection in the response. 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"
        }
    }
}

But when I call the URL for the sibling collection I get the sibling collection fine, just I want the sibling collection in the above response.

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

Solution

  • You can annotate your fields or getters with @JsonView(class) and after use same annotation with the same class on your controller method - it's better solution, or just annotate not expected fields with @JsonIgnore

    Here is more information about jackson configuration