Dependencies
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
- jackson-annotations-2.9.8.jar
- jackson-core-2.9.8.jar
- jackson-databind-2.9.8.jar
Change Name of Field for Serialization
1. Without using @JsonProperty Annotation
package net.javaguides.jackson.annotations;
public class User {
public int id;
private String firstName;
private String lastName;
private String fullName;
public User(int id, String firstName, String lastName, String fullName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.fullName = fullName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
package net.javaguides.jackson.annotations;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonPropertyAnnotationTest {
public static void main(String[] args) throws IOException {
// Create ObjectMapper object.
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare");
String result = mapper.writeValueAsString(bean);
System.out.println(result);
}
}
{
"id" : 1,
"firstName" : "Ramesh",
"lastName" : "Fadatare",
"fullName" : "Ramesh Fadatare"
}
2. Using @JsonProperty Annotation
{
"id" : 1,
"first_name" : "Ramesh",
"last_name" : "Fadatare",
"full_name" : "Ramesh Fadatare"
}
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
public int id;
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
@JsonProperty("full_name")
private String fullName;
public User(int id, String firstName, String lastName, String fullName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.fullName = fullName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
package net.javaguides.jackson.annotations;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonPropertyAnnotationTest {
public static void main(String[] args) throws IOException {
// Create ObjectMapper object.
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare");
String result = mapper.writeValueAsString(bean);
System.out.println(result);
}
}
{
"id" : 1,
"first_name" : "Ramesh",
"last_name" : "Fadatare",
"full_name" : "Ramesh Fadatare"
}
Related Articles
- Change Field Name in JSON using Jackson (popular)
GitHub Repository
The source code of this article available on my GitHub repository at https://github.com/RameshMF/jackson-json-tutorialContinue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment