【问题标题】:Genson property read errorGenson 属性读取错误
【发布时间】:2016-01-24 18:55:54
【问题描述】:

我使用 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 来处理 post 请求,我尝试使用 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(); 返回值.. 等等..

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

请帮帮我! 谢谢!

【问题讨论】:

    标签: java json web-services genson


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-10-23
      • 1970-01-01
      • 2021-11-07
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多