Feed it a Spring Data MongoDB @Document class together with its embedded classes and records, and it builds example JSON for what a controller returns when it sends that document. The shape follows Spring Boot's default Jackson serialization, so it describes the API response, not the BSON stored in MongoDB.
It saves time on MongoDB documents full of embedded lists, Map fields and Instant timestamps, where writing a response sample by hand is tedious, and it makes the gap between storage format and response format visible.
@Document class and its embedded classes or records into the Spring Data MongoDB box.id and date placeholders with realistic values before using the JSON in docs or tests.@Document(collection = "devices")
public class Device {
@Id
private String id;
private String serial;
@Field("fw")
private String firmwareVersion;
private Map<String, Double> lastReadings;
private List<Alert> alerts;
private Instant registeredAt;
}
record Alert(String level, String message, Instant raisedAt) {}{
"id": "",
"serial": "",
"firmwareVersion": "",
"lastReadings": {
"key": 0
},
"alerts": [
{
"level": "",
"message": "",
"raisedAt": "1970-01-01T00:00:00Z"
}
],
"registeredAt": "1970-01-01T00:00:00Z"
}Alert in List<Alert> is a record, so its components level, message and raisedAt become fields. Map<String, Double> becomes {"key": 0}, and Instant is a UTC string ending in Z. @Field("fw") only renames the stored field in MongoDB, so the JSON key stays firmwareVersion.
record Alert(String level, ...), are read as fields.List and Set become one-element arrays; Map<String, T> becomes {"key": value}.Instant, ZonedDateTime and OffsetDateTime give 1970-01-01T00:00:00Z, while LocalDateTime gives a string without an offset.ObjectId get a sample hex ObjectId string.@DBRef become nested objects when they are part of the input.@JsonIgnore, @JsonProperty, @JsonIgnoreProperties and inherited fields follow the same rules as the JPA page.Structures MongoDB packs into one document, like review lists, option maps and history arrays, make long response examples. Paste the classes and every nested level is written out for you.
Short names via @Field or fields mapped to _id make database keys differ from API keys. The output shows the API side, so you can put the two next to each other.
@Field("..."), @Indexed and @TextIndexed do not change JSON keys and are not reflected.@Id String id is stored as _id but leaves Jackson as id, which is what the output shows.org.bson.types.ObjectId as an object with timestamp and date, not as a hex string. To send the string form shown here, register a ToStringSerializer or use a String id.GeoJsonPoint, become null.@Id be ObjectId or String?With String, Spring Data converts it to an ObjectId on save and the API returns a clean hex string. Keeping the ObjectId type means you need extra Jackson configuration for responses.
@DBRef fields really come back as nested objects?@DBRef resolves the referenced document when reading (on first access if lazy), so responses contain a nested object. Manual references that store only an ObjectId or String id return just the id.
Map<String, Object> field look?Its value type is unknown, so it becomes {"key": null}. Replace it with the kind of value you actually store before putting it in docs.