इसे Java DTO, POJO या record source दें और वह JSON पाएं जो Spring Boot का default Jackson setup सबसे संभावित रूप से बनाएगा। Keys declared fields से आती हैं, और @JsonProperty, @JsonIgnore, @JsonIgnoreProperties व static/transient exclusions लागू होते हैं।
इसे controller के request या response DTO से Swagger example या frontend के लिए sample payload बनाने में इस्तेमाल करें, या बस यह जांचने के लिए कि Lombok @Data class कौन सी keys निकालेगी।
static class, record और enum declarations उसी इनपुट में शामिल करें, ताकि उन्हें reference करने वाले fields उनकी संरचना से भरें।रूट टाइप स्वचालित पर होने पर वह class चुनी जाती है जिसे कोई दूसरी class field type के रूप में इस्तेमाल नहीं करती। जब entities एक-दूसरे को reference करती हों, तो सूची से जो चाहिए वह चुनें।@Schema(example = ...), REST Docs snippet या MockMvc request body में कॉपी करें।@Data
@JsonIgnoreProperties({"password"})
public class MemberResponse {
private Long memberId;
@JsonProperty("nick_name")
private String nickname;
private String password;
private Grade grade = Grade.SILVER;
private LocalDate birthDate;
private List<Address> addresses;
private static final long serialVersionUID = 1L;
public enum Grade { BRONZE, SILVER, GOLD }
public record Address(String zipCode, String city, boolean primary) {}
}{
"memberId": 0,
"nick_name": "",
"grade": "SILVER",
"birthDate": "1970-01-01",
"addresses": [
{
"zipCode": "",
"city": "",
"primary": false
}
]
}Class-level @JsonIgnoreProperties({"password"}) से password हट जाता है, और static final serialVersionUID कभी नहीं दिखता। Initializer Grade.SILVER पढ़ा जाता है, इसलिए grade enum का पहला constant नहीं बल्कि "SILVER" है। Nested record Address objects के array में फैल जाता है।
@JsonProperty("x") या @JsonProperty(value = "x") override करता है।@JsonIgnore fields, class-level @JsonIgnoreProperties({...}) में सूचीबद्ध keys, और static या transient fields।record components, nested static class declarations और extends से inherit हुए fields (पहले parent fields) समर्थित हैं।@JsonProperty("x") string को x कर देता है।LocalDateTime → 1970-01-01T00:00:00, LocalDate → 1970-01-01, Instant/OffsetDateTime/ZonedDateTime → Z पर खत्म होने वाली ISO string, java.util.Date/Timestamp → 1970-01-01T00:00:00.000+00:00।BigDecimal और Long जैसे numeric wrappers 0 बनते हैं, Optional<T> खुलकर T बनता है, byte[] Base64 string बनता है, और UUID व MongoDB ObjectId को सही रूप वाली वैल्यू मिलती हैं।List, Set और arrays को एक sample element मिलता है; numeric key वाला Map<Long, V> जैसा map "0" इस्तेमाल करता है, वरना "key"।DTO में हर बार field जुड़ने पर हाथ से लिखी example values पीछे छूट जाती हैं। DTO source से JSON बनाएं और वहीं से values सुधारें, ताकि कोई key न छूटे और न गलत लिखी जाए।
@RequestBody DTO से सीधे request JSON बनाएं और उसे अपने test में text block (""") में पेस्ट करें। @JsonProperty से rename की गई keys पहले से लागू होती हैं, जो असली binding से मेल खाती हैं।
जो frontend डेवलपर्स backend कोड नहीं पढ़ते, उन्हें Java class समझने की बजाय सीधे endpoint की JSON संरचना मिल जाती है।
= "guest", = 100, = true) या enum constant (= Grade.SILVER) हों; बाकी कुछ भी, जैसे new ArrayList<>() या method call, type के placeholder पर लौट जाता है।getFullName() या boolean isVip(), Jackson की तरह properties के रूप में जोड़े जाते हैं, और उस getter पर लगे @JsonIgnore/@JsonProperty का पालन होता है। उल्टे, बिना getter वाला private field दिखने वाला माना जाता है (जैसे Lombok @Getter/@Data के साथ)।@JsonNaming(SnakeCaseStrategy.class), spring.jackson.property-naming-strategy) और @JsonFormat date patterns लागू नहीं होते।@JsonValue methods evaluate नहीं होते, इसलिए ऐसे enums फिर भी constant (या उसका @JsonProperty नाम) दिखाते हैं।Page<T>/Slice<T> fields को elements के array में सरल किया जाता है; असली Page serialization content, pageable आदि वाला object होता है।@Data या @Getter accessors बनाता है?हां। Keys declared fields से बनती हैं, इसलिए Lombok जो getters और setters compile time पर बनाता है, उनका source में होना ज़रूरी नहीं।
LocalDateTime संख्याओं का array नहीं बल्कि string क्यों है?Spring Boot jackson-datatype-jsr310 register करता है और default रूप से WRITE_DATES_AS_TIMESTAMPS बंद करता है, जिससे ISO strings बनती हैं; sample उसी default का पालन करता है। सादा Jackson ObjectMapper इसकी जगह [1970,1,1,0,0] जैसा array लिख सकता है।
Entities की field संरचना वही होती है, इसलिए वे ठीक से parse होती हैं। @OneToMany जैसे bidirectional associations loop बनाएंगे, इसलिए वे एक बार फैलाए जाते हैं और फिर null या खाली array से काट दिए जाते हैं। इसके लिए एक अलग Spring Boot JPA → JSON पेज भी है।
@JsonIgnore setter पर या मौजूदा field के getter पर हो तो?Field declaration पर लगे annotations हमेशा लागू होते हैं, और getter पर लगे annotations तभी लागू होते हैं जब उस नाम का कोई field मौजूद न हो। Setter पर, या declared field के getter पर लगा @JsonIgnore लागू नहीं होता, इसलिए असर देखने के लिए उसे field पर ले जाएं।