to put an object in a ObjectNode of Jackson

I am trying to set an Object in ObjectNode of Jackson and I am able to do that but I encountered a problem setting it into object node. the null attributes are also coming, using GSON I tried but then the " \" is coming in the request.

public class Testing4 {         public static void main(String[] args) throws JsonProcessingException {                      ObjectNode request = null;             ObjectMapper mapper = new ObjectMapper();             request = mapper.createObjectNode();                                               RequestParam requestParam  = new RequestParam();             requestParam.setCustomerName("sachin");             requestParam.setCustomerOrderNumber("12344556");                          request.set("request",mapper.convertValue(requestParam, JsonNode.class));             request.put("FatherName","jithin");                          String req = mapper.createObjectNode().set("request", request).toString();             System.out.println(req);                                                }          private static Comparator<? super String> kFirst() {           return (s1, s2) -> "k".equals(s1) ? -1 : "k".equals(s2) ? 1 : 0;          } } 

the DTO class

public class RequestParam implements Serializable {               /**      *       */     private static final long serialVersionUID = 1L;         private String orderDueDate;         private String originatingSystemOrderId;         private String objectId;         private String taskTypeId;         private String customerId;         private String customerName;         private String customerOrderNumber;         private Dataset dataset;   } 

here I can get the o/p but the o/p consist of null assignments as well how will I remove the null assignments the o/p I am getting now

{   "request": {     "request": {       "orderDueDate": null,       "originatingSystemOrderId": null,       "objectId": null,       "taskTypeId": null,       "customerId": null,       "customerName": "sachin",       "customerOrderNumber": "12344556",       "dataset": null     },     "FatherName": "jithin"   } } 

the o/p I want

{   "request": {     "request": {       "customerName": "sachin",       "customerOrderNumber": "12344556"     },     "FatherName": "jithin"   } } 

please help, thanks

Add Comment
2 Answer(s)

if @JsonInclude(Include.NON_NULL) dosent work then use

 @JsonInclude(JsonInclude.Include.NON_NULL) 

(@JsonSerialize(include = Inclusion.NON_NULL) don’t use this its deprecated) @JsonInclude(JsonInclude.Include.NON_NULL) public class RequestParam implements Serializable {

    private static final long serialVersionUID = 1L;         private String orderDueDate;         private String originatingSystemOrderId;         private String objectId;         private String taskTypeId;         private String customerId;         private String customerName;         private String customerOrderNumber;         private Dataset dataset;   } 
Add Comment
@JsonInclude(Include.NON_NULL) public class RequestParam implements Serializable {          private static final long serialVersionUID = 1L;         private String orderDueDate;         private String originatingSystemOrderId;         private String objectId;         private String taskTypeId;         private String customerId;         private String customerName;         private String customerOrderNumber;         private Dataset dataset;   } 

You need to add the import statement – import com.fasterxml.jackson.annotation.JsonInclude;

Add @JsonInclude(Include.NON_NULL) before the DTO class definition

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.