Enamul Haque
Enamul Haque

Reputation: 5053

Ignore json POJO fields not mapped in object in spring boot

I have pojo which has many fields. I have set value some field. But when i create json, whole pojo create a json . here is the code i have used:

  1. Pojo

    public class BasicInfoModel  {
      private String client_id="";
      private String father_name="";
      private String mother_name="";        
      private String gendar="";
     //Getter and setter
     }
    
  2. Repository code

     public BasicInfoModel getBasicInfo() {
        BasicInfoModel lm = new BasicInfoModel();
        lm.setFather_name("Enamul Haque");
        return lm;
       }
    
  3. Controller code

      @RequestMapping(value = "/get_donar_basic_info", method = RequestMethod.POST, produces ="application/json")   
      public @ResponseBody BasicInfoModel getBasicinfo(){
       return repository.getBasicInfo();
      }
    
  4. But my json is resposne like bellow:

    {
    "client_id": "",
    "father_name": "Enamul Haque",
    "mother_name": "",
    "gendar": ""
    }
    

    I have set value to father_name but i have seen that i have found all the value of pojo fields. I want get only set value of father_name and ignor other value which is not set in repository.

  5. My json look like bellow: I will display only father_name.how to display bellow like json from above code?

     { 
     "father_name": "Enamul Haque"
    
     }
    

    Please help me..

Upvotes: 0

Views: 3144

Answers (3)

Farrukh Ahmed
Farrukh Ahmed

Reputation: 493

You can ignore field at class level by using @JsonIgnoreProperties annotation and specifying the fields by name:

@JsonIgnoreProperties(value = { "client_id" })
public class BasicInfoModel  {
      private String client_id="";
      private String father_name="";
      private String mother_name="";        
      private String gendar="";
     //Getter and setter
     }

Or you can use @JsonIgnore annotation directly on the field.

public class BasicInfoModel  {
  @JsonIgnore
  private String client_id="";
  private String father_name="";
  private String mother_name="";        
  private String gendar="";
 //Getter and setter
 }

You can read here more about this.

Upvotes: 0

Ryuzaki L
Ryuzaki L

Reputation: 39978

Json include non null values to ignore null fields when serializing a java class

@JsonInclude(Include.NON_NULL)

Jackson allows controlling this behavior at either the class level:

@JsonInclude(Include.NON_NULL)
public class BasicInfoModel { ... }

at the field level:

public class BasicInfoModel  {
   private String client_id="";

   @JsonInclude(Include.NON_NULL)
   private String father_name="";

   private String mother_name="";   

   private String gendar="";

   //Getter and setter
}

from jackson 2.0 use here use

@JsonInclude(JsonInclude.Include.NON_NULL) 

You can also ignore the empty values

@JsonInclude(JsonInclude.Include.NON_EMPTY) 

Upvotes: 2

Hamza Khan
Hamza Khan

Reputation: 107

Add the @JsonIgnoreProperties("fieldname") annotation to your POJO.

Or you can use @JsonIgnore before the name of the field you want to ignore while deserializing JSON. Example:

 @JsonIgnore
 @JsonProperty(value = "client_id")
 @RequestMapping(value = "/get_donar_basic_info", method = RequestMethod.POST, produces ="application/json")   
 public @ResponseBody BasicInfoModel getBasicinfo(){
  return repository.getBasicInfo();
 }

Upvotes: 0

Related Questions