0

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"
                    }
                }
            }
        ]
    }
}
Nick N
  • 984
  • 9
  • 22
  • Does anyone know what generates the "_links": section? I'm wondering if this is generated from spring boot or the fasterxml jackson library. – Nick N Jul 04 '15 at 00:40

2 Answers2

1

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

kxyz
  • 802
  • 1
  • 9
  • 32
  • Thanks for the response. I noticed that implementation, but wasn't sure on it. 'll give that a try. Thanks again. – Nick N Jul 03 '15 at 23:44
1

The answer from kxyz gave me the solution, bu I wanted to give more explicit details to the end solution for others.

I didn't use the @JsonManagedReference and @JsonBackReference annotations.

I had to create the following view:

public class StationView {

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

}

And the controller (instead of using the spring boot mystery controller that must be generated on start), so it could be annotated with this new 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);
    }

}

I annoated each class variable to be exposed by JSON on both parent class and sibling class.

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

and the sibling collection the same way.

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

And I get something like this from the JSON response.

[
    {
        "stationId": 1,
        "stationName": "station name 1",
        "active": true,
        "stationTanks":
        [
            {
                "stationTankId": 1,
                "active": true
            },
            {
                "stationTankId": 2,
                "active": true
            }
        ]
    }
]
Nick N
  • 984
  • 9
  • 22