java123999
java123999

Reputation: 7394

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.

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.

Stub Json:

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

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:

@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

actionCode = "Create"

    alternateOrderIdentifier = null

    from - null

    status = "Open"

    orderIdentifier = null

    customerId = null

Obviously the Object is not being mapped correctly.

I am expecting e.g. CustomerId to be null, as I havent added it to the Stub Json. But I am not sure why I am getting nulls for e.g. orderIdentifier and alternateOrderIdentifier?

I am sure that the path and the file name of the json file are correct. What could the issue be?

Upvotes: 0

Views: 741

Answers (2)

Mosd
Mosd

Reputation: 1682

The correct stub json for the class Order should be:

{"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

Upvotes: 1

Artyom Rebrov
Artyom Rebrov

Reputation: 691

Considering your stub Json, the java object should be of the following structure.

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+"]";
    }
}

Upvotes: 0

Related Questions