简体   繁体   English

Gson.toJson()返回带有空字段的默认对象?

[英]Gson.toJson() returning default object with null fields?

Within my Java Handler class, I am trying to use the Gson.toJson() method in order to return stub data as a response. 在我的Java Handler类中,我试图使用Gson.toJson()方法以返回存根数据作为响应。

The issue I am having is that the Gson.toJson() method is not correctly mapping my Json , what it is doing is giving an object with null fields and default values. 我遇到的问题是Gson.toJson()方法未正确映射我的Json ,它正在做的是给一个具有空字段和默认值的对象。

Stub Json: 存根Json:

  {
    "order": [{
        "from": "exampleCustomer",
        "alternateOrderIdentifier": [{
                "type": "Service Type",
                "value": "order"
            },
            {
                "type": "Product Type",
                "value": "book"
            }
        ],
        "orderIdentifier": "order123"
    }]
  }

Java POJO: Java POJO:

public class Order {

    private String actionCode = "Create";

    private AlternateOrderIdentifier[] alternateOrderIdentifier;

    private String from;

    private String status = "Open";

    private String orderIdentifier;

    private String customerId;

    public Order() {
    }

   //getters and setters

   }

Java Handler method: Java处理程序方法:

@GET
    @Path("/my/path/order")
    public Response getOrderDetails(@QueryParam("orderId") String orderIdentifier
                                         ) throws IOException {

        Order order = new Order();
        try {
            InputStream is = StubOrderHandler.class.getResourceAsStream("/my/path/to/stubOrder.json");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
            Gson gson = new Gson();
            order = gson.fromJson(bufferedReader, Order.class);
        } catch (Exception e) {

        }
        return ResponseBuilder.anOKResponse().withEntity(order).build();
    }   

The Order object fields I am getting returned in my response are as follows 我在响应中返回的Order对象字段如下

actionCode = "Create"

    alternateOrderIdentifier = null

    from - null

    status = "Open"

    orderIdentifier = null

    customerId = null

Obviously the Object is not being mapped correctly. 显然,对象未正确映射。

I am expecting eg CustomerId to be null, as I havent added it to the Stub Json. 我期望例如CustomerId为null,因为我还没有将其添加到Stub Json。 But I am not sure why I am getting nulls for eg orderIdentifier and alternateOrderIdentifier? 但是我不确定为什么为什么要为例如orderIdentifier和AlternateOrderIdentifier获得null?

I am sure that the path and the file name of the json file are correct. 我确定json文件的路径和文件名正确。 What could the issue be? 可能是什么问题?

The correct stub json for the class Order should be: 类Order的正确stub json应该是:

{"alternateOrderIdentifier":[{"type":"Service Type","value":"order"}],"from":"exampleCustomer","orderIdentifier":"order123"}

And not as what you have...basically you need to either change the stub json or you change the class you using to de-serialize/serialize 而不是您所拥有的...基本上,您需要更改存根json或更改用于反序列化/序列化的类

Considering your stub Json, the java object should be of the following structure. 考虑到您的存根Json,java对象应具有以下结构。

public class MyPojo {
    private Order[] order;

    public Order[] getOrder () {
        return order;
    }

    public void setOrder (Order[] order) {
        this.order = order;
    }

    @Override
    public String toString() {
        return "ClassPojo [order = "+order+"]";
    }
}

public class Order {
    private AlternateOrderIdentifier[] alternateOrderIdentifier;

    private String orderIdentifier;

    private String from;

    public AlternateOrderIdentifier[] getAlternateOrderIdentifier () {
        return alternateOrderIdentifier;
    }

    public void setAlternateOrderIdentifier (AlternateOrderIdentifier[] alternateOrderIdentifier) {
        this.alternateOrderIdentifier = alternateOrderIdentifier;
    }

    public String getOrderIdentifier () {
        return orderIdentifier;
    }

    public void setOrderIdentifier (String orderIdentifier) {
        this.orderIdentifier = orderIdentifier; 
    }

    public String getFrom () {
        return from;
    }

    public void setFrom (String from) {
        this.from = from;
    }

    @Override
    public String toString() {
        return "ClassPojo [alternateOrderIdentifier = "+alternateOrderIdentifier+", orderIdentifier = "+orderIdentifier+", from = "+from+"]";
    }
}

public class AlternateOrderIdentifier {
    private String value;

    private String type;

    public String getValue () {
        return value;
    }

    public void setValue (String value) {
        this.value = value;
    }

    public String getType () {
        return type;
    }

    public void setType (String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "ClassPojo [value = "+value+", type = "+type+"]";
    }
}

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

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