我使用 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 {
...
...
我的类(class)里也有 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
.
请帮助我! 谢谢你!
最佳答案
我认为这是由于这个名字。在您的 json 中,我们可以看到您在单词边界处使用下划线而不是大写字母。就像 due_date 而不是 dueDate。我认为代码中的属性遵循通常的大写 Java 命名约定。
一种解决方案是使用 @JsonProperty 注释这些 set 和 get 方法,以将名称从“dueDate”更改为“due_date”。
顺便说一句,生成的代码不适用于 Genson,JsonPropertyOrder 不是 Genson 注释。
关于java - Genson 属性读取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33332813/