I have three classes in my Java project:
@Document(collection = "foos")
public class Foo {
@Id
private String id;
private String foo_name;
private Set<Bar> bars;
}
@Document(collection = "bazs")
public class Baz {
@Id
private String id;
private String baz_name;
}
public class Bar {
@DBRef
private Baz baz;
private String bar_name;
}
Then, I have /foo POST endpoint that accepts the body of a foo and stores it in a database.
@PostMapping
public Foo addFoo(@RequestBody Foo foo) {
return repository.save(foo);
}
Given a Baz already stored in mongoDB with the id = ObjectId('62b795ea78aa537285762a1f') and name = the baz name, and I can call the /foo endpoint with the following body:
{ "foo_name" = "the foo name", "bars"= ["baz"= { "id" = "62b795ea78aa537285762a1f" }, "bar_name" = "the bar name"] }
I get a 200 OK, but the response looks like this:
{ "id" = "62b6e461cf1993ef423a28b3", "foo_name" = "the foo name", "bars"= ["baz"= { "id" = "62b795ea78aa537285762a1f", "baz_name" = null } , "bar_name" = "the bar name"]
If I use the GET endpoint /foo/62b6e461cf1993ef423a28b3, I do get all the @DBRef info:
{ "id" = "62b6e461cf1993ef423a28b3", "foo_name" = "the foo name", "bars"= ["baz"= { "id" = "62b795ea78aa537285762a1f", "baz_name" = "the baz name" } , "bar_name" = "the bar name"]
Note the difference in the baz_name variable.
How can I make my code return all the data once I save a new item foo? Or, at least, don't send null values that are not real