इसे Spring Data MongoDB @Document class और उसकी embedded classes व records दें, और यह उस JSON का उदाहरण बनाता है जो controller वह document भेजते समय लौटाता है। आकार Spring Boot के default Jackson serialization का पालन करता है, इसलिए यह API response बताता है, MongoDB में stored BSON नहीं।
यह embedded lists, Map fields और Instant timestamps से भरे MongoDB documents पर समय बचाता है, जहां हाथ से response sample लिखना थकाऊ है, और storage format व response format के बीच का अंतर दिखाता है।
@Document class और उसकी embedded classes या records को Spring Data MongoDB बॉक्स में पेस्ट करें।id और date placeholders को असली जैसी values से बदलें।@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"
}List<Alert> का Alert एक record है, इसलिए उसके components level, message और raisedAt fields बनते हैं। Map<String, Double> {"key": 0} बनता है, और Instant Z पर खत्म होने वाली UTC string है। @Field("fw") सिर्फ़ MongoDB में stored field rename करता है, इसलिए JSON key firmwareVersion ही रहती है।
record Alert(String level, ...) जैसे Java record components fields के रूप में पढ़े जाते हैं।List और Set एक element वाले arrays बनते हैं; Map<String, T> {"key": value} बनता है।Instant, ZonedDateTime और OffsetDateTime 1970-01-01T00:00:00Z देते हैं, जबकि LocalDateTime बिना offset वाली string देता है।ObjectId type वाले fields को sample hex ObjectId string मिलती है।@DBRef से जुड़ी classes इनपुट का हिस्सा हों तो nested objects बनती हैं।@JsonIgnore, @JsonProperty, @JsonIgnoreProperties और inherit हुए fields JPA पेज वाले नियमों का ही पालन करते हैं।MongoDB जिन संरचनाओं को एक document में समेटता है, जैसे review lists, option maps और history arrays, उनसे लंबे response examples बनते हैं। Classes पेस्ट करें और हर nested स्तर आपके लिए लिख दिया जाता है।
@Field से छोटे नाम या _id से map हुए fields database keys को API keys से अलग बना देते हैं। आउटपुट API वाली तरफ़ दिखाता है, इसलिए आप दोनों को साथ-साथ रख सकते हैं।
@Field("..."), @Indexed और @TextIndexed जैसे mapping annotations JSON keys नहीं बदलते और प्रतिबिंबित नहीं होते।@Id String id _id के रूप में store होता है लेकिन Jackson से id के रूप में निकलता है, और आउटपुट यही दिखाता है।org.bson.types.ObjectId को hex string नहीं, बल्कि timestamp और date वाले object के रूप में लिखता है। यहां दिखाया string रूप भेजने के लिए ToStringSerializer register करें या String id इस्तेमाल करें।GeoJsonPoint जैसे Spring Data types समेत, null बनती हैं।@Id ObjectId हो या String?String के साथ Spring Data save करते समय उसे ObjectId में बदलता है और API साफ़ hex string लौटाता है। ObjectId type रखने का मतलब है responses के लिए अतिरिक्त Jackson configuration चाहिए।
@DBRef fields सच में nested objects के रूप में लौटते हैं?@DBRef पढ़ते समय (lazy हो तो पहली access पर) reference किया document resolve करता है, इसलिए responses में nested object होता है। सिर्फ़ ObjectId या String id store करने वाले manual references सिर्फ़ id लौटाते हैं।
Map<String, Object> field कैसा दिखता है?उसकी value type अज्ञात है, इसलिए वह {"key": null} बनता है। Docs में डालने से पहले उसे उस तरह की वैल्यू से बदलें जो आप असल में store करते हैं।