Jersey JAX-RS 3 级嵌套对象具有空值

Jersey JAX-RS 3 level nested object has null value

我有以下 JSON 代码需要解析。我正在使用相应的 JAX-RS 模型。问题是 paymillClient 对象为空。如果我将 currency 作为字符串添加到 PaymillSubscription 对象中,则它是 returns EUR 值,而不是 null。所以 PaymillClient 对象似乎有问题,而不是纯字符串。是否可以限制要解析的嵌套对象的数量?例如,最多 2 个嵌套对象。所以因为在我的情况下有 3 个,所以它不起作用。

不幸的是,我根本无法更改需要解析的JSON代码。我只需要让它与 JAX-RS 实现一起工作。

{  
   "event":{  
      "event_type":"subscription.succeeded",
      "event_resource":{  
         "subscription":{  
            "id":"sub_29f144a3bc32c71f96e2",
            "offer":{  },
            "livemode":false,
            "amount":200,
            "temp_amount":null,
            "currency":"EUR",
            "name":"Monthly subscription",
            "interval":"1 MONTH",
            "trial_start":null,
            "trial_end":null,
            "period_of_validity":null,
            "end_of_period":null,
            "next_capture_at":1428939744,
            "created_at":1426264944,
            "updated_at":1426264944,
            "canceled_at":null,
            "payment":{  },
            "app_id":null,
            "is_canceled":false,
            "is_deleted":false,
            "status":"active",
            "client":{ 
              "id":"client_c0c24aa7f97e1b8ed15d" 
            }
         },
         "transaction":{  }
      },
      "created_at":1426264944,
      "app_id":null
   }
}

PaymillEventContainer:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class PaymillEventContainer
{
    private PaymillEvent event;
}

PaymillEvent:

@XmlAccessorType(XmlAccessType.FIELD)
public class PaymillEvent
{
    @XmlElement(name = "event_type") @DocumentationExample(value = "subscription.succeeded") private String eventType;
    @XmlElement(name = "event_resource") private PaymillEventResource eventResource;
}

PaymillEventResource:

@XmlAccessorType(XmlAccessType.FIELD)
public class PaymillEventResource
{
    private PaymillClient client;
    private PaymillOffer offer;
    private PaymillSubscription subscription;
}

Paymill订阅:

@XmlAccessorType(XmlAccessType.FIELD)
public class PaymillSubscription
{
    private PaymillClient client;
    private PaymillOffer offer;
}

PaymillClient:

@XmlAccessorType(XmlAccessType.FIELD)
public class PaymillClient
{
    @DocumentationExample(value = "client_c0c24aa7f97e1b8ed15d") private String id;
}

API端点代码:

public Response postSubscriptionSucceeded(PaymillEventContainer paymillEventContainer)
    {
        PaymillEvent paymillEvent = paymillEventContainer.getPaymillEvent();
        PaymillEventResource paymillEventResource = paymillEvent.getEventResource();

        PaymillSubscription paymillSubscription = paymillEventResource.getSubscription();
        PaymillClient paymillClient = paymillSubscription.getPaymillClient();
        PaymillOffer paymillOffer = paymillSubscription.getPaymillOffer();

        String clientId = paymillClient.getId(); // NullPointerException
}

好的。我试图在你的机器上 运行 你的代码,但也收到了空值(注意,我正在使用 MOXy 解组 JSON)。然后,我试着用它做了一点实验,发现了一些非常有趣的事情:

1. 如果您将从 JSON 中删除所有空值字段,一切都会完美无缺。

2. 如果您将向 PaymillSubscription 添加另一个字段。我添加了私人测试测试,其中测试是:

@XmlAccessorType(XmlAccessType.FIELD)
public class Test {
    private String id;
}

并将在订阅对象中的最后一个空值字段和 "client" 字段之间发送此 "test" 对象:

"test":{"id":"sadas"},
"client":{ 
    "id":"client_c0c24aa7f97e1b8ed15d"
 }

那么 "test" 将为 null,但 "client" 将按预期进行解析。

3. 如果您将所有空值对象添加到模型中(我的意思是,在 PaymillSubscription class 中创建相应的字段)所有工作都非常完美。

似乎,默认情况下 JAXB 规范不允许 JSON 具有无法识别的字段,但 MOXy 仍会尝试解析它(有时会产生错误)。