Sebastian Ullrich
Sebastian Ullrich

Reputation: 1047

Spring Data Rest POST response different from GET response

On my Spring Data Rest project I have a Competition entity that references an GeoLocation entity:

public class Competition {
    @Id
    private String uname;

    [...]

    @NotNull
    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    private GeoLocation geoLocation;
}

public class GeoLocation {
    @Id private Long id;
    private Double latitude;
    private Double longitude;
}

Since every Competition must have a GeoLocation defined, the Competition entity handles the creation via cascade. When creating a new Competition entity via POST, I get the following response:

{
    "uname": "Some Competition",
    "geoLocation": {
        [content of geoLocation]
    },
    "_links": {
        [...]
    }
}

But when I call the newly created competition, the content of the GeoLocation will be wrapped in a content field.

{
    "uname": "Some Competition",
    "geoLocation": {
        "content": {
            [content of geoLocation]
        }        
    },
    "_links": {
        [...]
    }
}

I would expect that both requests would deliver the same response?

Upvotes: 1

Views: 73

Answers (1)

Sebastian Ullrich
Sebastian Ullrich

Reputation: 1047

@JsonUnwrapped solved this issue for me:

public class Competition {
    @Id
    private String uname;

    [...]

    @NotNull
    @JsonUnwrapped
    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    private GeoLocation geoLocation;
}

Upvotes: 1

Related Questions