Jackson library provides a @JsonPropertyOrder annotation to specify the order of properties on serialization.
Dependencies
Let’s first add the following dependencies to the pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
This dependency will also transitively add the following libraries to the classpath:
- jackson-annotations-2.9.8.jar
- jackson-core-2.9.8.jar
- jackson-databind-2.9.8.jar
Always use the latest versions on the Maven central repository for Jackson databind.
Jackson @JsonPropertyOrder Example
Let’s set a custom order for the properties of a User entity:
User.java
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonPropertyOrder({
"fullName",
"id",
"firstName",
"lastName"
})
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;
}
}
Let's test usage of @JsonPropertyOrder annotation using a main() method.
JacksonPropertyOrderDemo.java
package net.javaguides.jackson.annotations;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JacksonPropertyOrderDemo{
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);
}
}
Output:
{
"fullName" : "Ramesh Fadatare",
"id" : 1,
"firstName" : "Ramesh",
"lastName" : "Fadatare"
}
Order the Properties Alphabetically
We can also use @JsonPropertyOrder(alphabetic=true) to order the properties alphabetically.
@JsonPropertyOrder(alphabetic=true)
public class User {
private int id;
private String firstName;
private String lastName;
private String fullName;
And in that case the output of serialization will be:
{
"firstName" : "Ramesh",
"fullName" : "Ramesh Fadatare",
"id" : 1,
"lastName" : "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-tutorial
Comments
Post a Comment
Leave Comment