java / beginner
Snippet
Renaming JSON Fields with @JsonProperty
@JsonProperty allows you to define a custom name for a field in the generated JSON, decoupling Java names from the API contract.
snippet.java
1
2
3
4
public class UserResponse {@JsonProperty("user_name")private String username;}
spring
Breakdown
1
@JsonProperty("user_name")
Maps the Java field 'username' to 'user_name' in the JSON output.
2
private String username;
The internal Java field name, which can follow camelCase conventions.