logo

Jackson은 Null 필드를 무시합니다.

이번 절에서는 Jackson을 사용하여 Java 클래스를 직렬화할 때 null 필드를 무시하는 방법을 알아봅니다.

클래스 수준, 필드 수준 또는 전역적으로 null 필드를 무시할 수 있습니다. 하나하나씩 이해해 봅시다.

클래스 수준에서 null 필드 무시

클래스 수준에서 null 필드를 무시하기 위해 다음을 사용합니다. @JsonInclude 주석을 달다 include.NON_NULL . @JsonInclude 주석을 사용하여 클래스 수준에서 null 필드를 무시하는 방법을 이해하기 위해 예를 들어 보겠습니다.

IgnoreNullFieldExample1.java

 //import required classes and package if any import java.io.IOException; import java.util.Scanner; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; //create class IgnoreNullFieldExample1 to understand how we can ignore the null field on marshalling public class IgnoreNullFieldExample1 { //main() methods start public static void main(String args[]) { // create an instance of ObjectMapper class ObjectMapper mapper = new ObjectMapper(); // create Scanner class object Scanner sc= new Scanner(System.in); // using try-catch try { College colg; String name, university, street, state, city, rank; System.out.println('Enter College Name:'); name = sc.nextLine(); System.out.println('Enter University Name:'); university = sc.nextLine(); System.out.println('Enter Street:'); street = sc.nextLine(); System.out.println('Enter City:'); city = sc.nextLine(); System.out.println('Enter State:'); state = sc.nextLine(); System.out.println('Enter College Rank:'); rank = sc.nextLine(); // set values to College object by using constructor colg = new College(name, university, new Address(street, state, null), null); // serialize College using mapper String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(colg); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } //close Scanner class object sc.close(); } } //create class College by using @JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_NULL) class College { //Creating properties of College class public String name; public String university; public Address address; public String rank; // constructor College(String name, String university, Address address, String rank){ this.name = name; this.university = university; this.address = address; this.rank = rank; } } //create class Address @JsonInclude(JsonInclude.Include.NON_NULL) class Address { public String street; public String state; public String city; Address(String street, String state, String city){ this.street = street; this.state = state; this.city = city; } } 

산출:

Jackson은 Null 필드를 무시합니다.

특정 null 필드 무시

여기서 우리는 @JsonInclude 주석을 달다 include.NON_NULL 특정 null 필드를 무시하려면 필드 수준에서 사용하세요. @JsonInclude 주석을 사용하여 특정 null 필드를 무시하는 방법을 이해하기 위해 예를 들어 보겠습니다.

IgnoreNullFieldExample2.java

 //import required classes and package if any import java.io.IOException; import java.util.Scanner; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; //create class IgnoreNullFieldExample2 to understand how we can ignore specific null fields on marshalling public class IgnoreNullFieldExample2 { //main() methods start public static void main(String args[]) { // create an instance of ObjectMapper class ObjectMapper mapper = new ObjectMapper(); // create Scanner class object Scanner sc= new Scanner(System.in); // using try-catch try { College colg; String name, university, street, state, city, rank; System.out.println('Enter College Name:'); name = sc.nextLine(); System.out.println('Enter University Name:'); university = sc.nextLine(); System.out.println('Enter Street:'); street = sc.nextLine(); System.out.println('Enter City:'); city = sc.nextLine(); System.out.println('Enter State:'); state = sc.nextLine(); System.out.println('Enter College Rank:'); rank = sc.nextLine(); // set values to College object by using constructor colg = new College(name, university, new Address(street, state, city), null); // serialize College using mapper String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(colg); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } //close Scanner class object sc.close(); } } //create class College by using @JsonInclude(JsonInclude.Include.NON_NULL) class College { //Creating properties of College class public String name; public String university; public Address address; @JsonInclude(JsonInclude.Include.NON_NULL) public String rank; // constructor College(String name, String university, Address address, String rank){ this.name = name; this.university = university; this.address = address; this.rank = rank; } } //create class Address class Address { public String street; public String state; public String city; Address(String street, String state, String city){ this.street = street; this.state = state; this.city = city; } } 

산출:

Jackson은 Null 필드를 무시합니다.

전역적으로 null 필드 무시

여기서는 ObjectMapper 클래스의 JsonInclude.Include.NON_NULL을 사용하여 전역적으로 null 필드를 무시하는 동작을 구성합니다. 우리는 setSerializationInclusion() 방법 포함.NON_NULL 다음과 같은 방법으로:

 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) 

다음을 사용하여 전역적으로 null 필드를 무시할 수 있는 방법을 이해하는 예를 들어보겠습니다. setSerializationInclusion() 방법.

TCP IP 모델

IgnoreNullFieldExample3.java

 //import required classes and package if any import java.io.IOException; import java.util.Scanner; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; //create class IgnoreNullFieldExample3 to understand how we can ignore null fields globally public class IgnoreNullFieldExample3 { //main() methods start public static void main(String args[]) { // create an instance of ObjectMapper class ObjectMapper mapper = new ObjectMapper(); // create Scanner class object Scanner sc= new Scanner(System.in); // using try-catch try { College colg; String name, university, street, state, city, rank; System.out.println('Enter College Name:'); name = sc.nextLine(); System.out.println('Enter University Name:'); university = sc.nextLine(); System.out.println('Enter Street:'); street = sc.nextLine(); System.out.println('Enter City:'); city = sc.nextLine(); System.out.println('Enter State:'); state = sc.nextLine(); System.out.println('Enter College Rank:'); rank = sc.nextLine(); // set values to College object by using constructor colg = new College(name, university, new Address(street, state, null), null); // serialize College using mapper String jsonString = mapper .setSerializationInclusion(JsonInclude.Include.NON_NULL) .writerWithDefaultPrettyPrinter() .writeValueAsString(colg); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } //close Scanner class object sc.close(); } } //create class College class College { //Creating properties of College class public String name; public String university; public Address address; public String rank; // constructor College(String name, String university, Address address, String rank){ this.name = name; this.university = university; this.address = address; this.rank = rank; } } //create class Address class Address { public String street; public String state; public String city; Address(String street, String state, String city){ this.street = street; this.state = state; this.city = city; } } 

산출:

Jackson은 Null 필드를 무시합니다.

위의 모든 방법은 null 필드를 무시하는 데 사용되며 각 방법은 다른 경우에 사용됩니다.