简体   繁体   English

将 object 放入 Jackson 的 ObjectNode

[英]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.我正在尝试在ObjectObjectNode中设置 Object 并且我能够做到这一点,但是在将其设置到 object 节点时遇到了问题。 the null attributes are also coming, using GSON I tried but then the " \" is coming in the request. null属性也即将到来,我尝试使用 GSON 但随后请求中出现了" \"

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 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在这里我可以得到 o/p 但 o/p 包括null分配以及我将如何删除null分配我现在得到的 o/p

{
  "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我想要的 o/p

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

please help, thanks请帮忙,谢谢

if @JsonInclude(Include.NON_NULL) dosent work then use如果@JsonInclude(Include.NON_NULL)有效,则使用

 @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 { (@JsonSerialize(include = Inclusion.NON_NULL)不要使用它已弃用)@JsonInclude(JsonInclude.Include.NON_NULL) public class RequestParam 实现可序列化 {

    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;


}
@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;需要添加import语句import com.fasterxml.jackson.annotation.JsonInclude;

Add @JsonInclude(Include.NON_NULL) before the DTO class definition在 DTO class 定义之前添加@JsonInclude(Include.NON_NULL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM