1

我使用http://www.jsonschema2pojo.org/从 json 模板创建了一个类,并使用 Genson 将我的 json 映射到基于 Jersey 的 WS。这是我的“json 类”的第一行:

@JsonPropertyOrder({
    "public_key",
    "template",
    "signature",
    "due_date",
    "fulfillment_date",
    "template_lang_code",
    "clients_id",
    "electronic_invoice",
    "is_draft",
    "recurring_time",
    "comment",
    "currency",
    "items",
    "payment_method",
    "ts"
})
public class CreateInvoiceBean {
...
...

我的班上也有 getter 和 setter。

我创建了一个 restfull Ws 来处理发布请求,并且我尝试使用 firefox RESTClinent 插件发送 jsons 对象。

这是我尝试发送的 json 对象的第一行:

{
    "public_key": "7f566499549fc9e6d9cc69ca3b10d5f5",
    "template": "billingo",
    "signature": "9273882e8b3bc7f57e1ef3bc10041bc4bf9d835c152a1e0b810b77b3d51864ad",
    "due_date": "2015-10-30", 
...
...}

我的 WS Post 处理程序方法如下所示:

 @POST
 @Path("/invoice")
 @Consumes("application/json")
 @Produces("application/json")
 public String createInvoice(CreateInvoiceBean newBillingoInvoice) {

     LOG.info("invoicenum:. " +  newBillingoInvoice.getDueDate());

     return newBillingoInvoice.getDueDate();
 }

我的请求到达,并且createInvoice()调用了该方法,但是如果我调用newBillingoInvoice.getDueDate()它,则返回 null,但是当我调用newBillingoInvoice.getSignature()它时,它会返回我在请求 json 中发送的值。等等。如果我调用newBillingoInvoice.getXY();返回null,并且如果我调用newBillingoInvoice.getOtherSomething();返回值.. ETC..

我的问题是,一个属性在同一个对象中null而另一个不在null同一个对象中怎么会发生?当我创建请求时,我设置了所有属性都没有null

请帮我!谢谢!

4

1 回答 1

1

这是由于我认为的名字。在您的 json 中,我们可以看到您在单词边界处使用了大写的下划线。像due_date 而不是dueDate。而且我认为您的代码中的属性遵循通常的大写 java 命名约定。

一种解决方案是使用 @JsonProperty 注释这些设置并获取将名称从“dueDate”更改为“due_date”的方法。

顺便说一句,生成的代码不适用于 Genson,JsonPropertyOrder 不是 Genson 注释。

于 2015-10-25T21:18:52.153 回答