Following example shows how to use @JsonIgnore and @JsonIgnoreProperties annotations to ignore properties.
@JsonIgnore Example
@JsonIgnore can be used on fields or getters or setters to ignore individual properties.
public class Employee {
private String name;
private String dept;
@JsonIgnore
private String address;
.............
}
public class ExampleMain {
public static void main(String[] args) throws IOException {
Employee employee = new Employee();
employee.setName("Trish");
employee.setDept("Admin");
employee.setAddress("421 Moon Hill");
//convert to json
String jsonString = toJson(employee);
System.out.println(jsonString);
//convert to object;
Employee e = toEmployee(jsonString);
System.out.println(e);
//address here will be ignore too
String s = "{\"name\":\"Trish\",\"dept\":\"Admin\", \"address\":\"xyz Street\"}";
System.out.println("JSON input: "+s);
Employee e2 = toEmployee(s);
System.out.println(e2);
}
private static Employee toEmployee(String jsonData) throws IOException {
ObjectMapper om = new ObjectMapper();
return om.readValue(jsonData, Employee.class);
}
private static String toJson(Employee employee) throws IOException {
ObjectMapper om = new ObjectMapper();
return om.writeValueAsString(employee);
}
} {"name":"Trish","dept":"Admin"} Employee{name='Trish', dept='Admin', address='null'} JSON input: {"name":"Trish","dept":"Admin", "address":"xyz Street"} Employee{name='Trish', dept='Admin', address='null'}
@JsonIgnoreProperties Example
This annotation can be used to ignore multiple properties with one declaration:
@JsonIgnoreProperties({"dept", "address"})
public class Employee2 {
private String name;
private String dept;
private String address;
.............
}
public class ExampleMain2 {
public static void main(String[] args) throws IOException {
Employee2 employee = new Employee2();
employee.setName("Trish");
employee.setDept("Admin");
employee.setAddress("421 Moon Hill");
//convert to json
String jsonString = toJson(employee);
System.out.println(jsonString);
//convert to object;
Employee2 e = toEmployee(jsonString);
System.out.println(e);
//address/dept here will be ignored even they are in the source JSON
String s = "{\"name\":\"Trish\",\"dept\":\"Admin\", \"address\":\"xyz Street\"}";
System.out.println("JSON input: "+s);
Employee2 e2 = toEmployee(s);
System.out.println(e2);
}
private static Employee2 toEmployee(String jsonData) throws IOException {
ObjectMapper om = new ObjectMapper();
return om.readValue(jsonData, Employee2.class);
}
private static String toJson(Employee2 employee) throws IOException {
ObjectMapper om = new ObjectMapper();
return om.writeValueAsString(employee);
}
} {"name":"Trish"} Employee{name='Trish', dept='null', address='null'} JSON input: {"name":"Trish","dept":"Admin", "address":"xyz Street"} Employee{name='Trish', dept='null', address='null'}
Ignoring Unknown Properties
@JsonIgnoreProperties#ignoreUnknown can be used to ignore a JSON property if is not defined in the POJO:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Employee3 {
private String name;
private String dept;
private String address;
.............
}
public class ExampleMain3 {
public static void main(String[] args) throws IOException {
String jsonString = "{\"name\":\"Trish\",\"phone\":\"111-111-1111\"}";
System.out.println(jsonString);
//convert to object;
Employee3 e = toEmployee(jsonString);
System.out.println(e);
}
private static Employee3 toEmployee(String jsonData) throws IOException {
ObjectMapper om = new ObjectMapper();
return om.readValue(jsonData, Employee3.class);
}
} {"name":"Trish","phone":"111-111-1111"} Employee{name='Trish', dept='null', address='null'}
Without ignoreUnknown = true , we will have following exception:
Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "phone" (class com.logicbig.example.Employee3), not marked as ignorable (3 known properties: "name", "address", "dept"])
at [Source: (String)"{"name":"Trish","phone":"111-111-1111"}"; line: 1, column: 26] (through reference chain: com.logicbig.example.Employee3["phone"])
at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1545)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:293)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
at com.logicbig.example.ExampleMain3.toEmployee(ExampleMain3.java:18)
at com.logicbig.example.ExampleMain3.main(ExampleMain3.java:12)
Example ProjectDependencies and Technologies Used: - jackson-databind 2.9.3: General data-binding functionality for Jackson: works on core streaming API.
- JDK 9
- Maven 3.3.9
|