在我的Java
处理程序类中,我尝试使用Gson.toJson()
方法以返回存根数据作为响应。
我遇到的问题是Gson.toJson()
方法没有正确映射我的Json
,它正在做的是给一个具有空字段和默认值的对象。
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处理程序方法:
@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();
}
我在回复中返回的订单对象字段如下
actionCode = "Create"
alternateOrderIdentifier = null
from - null
status = "Open"
orderIdentifier = null
customerId = null
显然,Object未正确映射。
我期待例如CustomerId为null,因为我没有将它添加到Stub Json。但是我不知道为什么我会因为orderIdentifier和alternateOrderIdentifier?
我确信json文件的路径和文件名是正确的。问题是什么?
答案 0 :(得分:1)
Order类的正确存根json应为:
{"alternateOrderIdentifier":[{"type":"Service Type","value":"order"}],"from":"exampleCustomer","orderIdentifier":"order123"}
而不是你拥有的...基本上你需要更改存根json或者你改变你用来反序列化/序列化的类
答案 1 :(得分:0)
考虑到你的存根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+"]";
}
}