I created a class from a json template with http://www.jsonschema2pojo.org/ and I use Genson to map my json with a Jersey based WS. This is the first lines of my "json class" :
@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 {
...
...
I have getters and setters also in my class too.
I have created a restfull Ws to handle post requests and i tried to send jsons object with firefox RESTClinent plugin.
This is the first lines of my json object that i tried to send:
{
"public_key": "7f566499549fc9e6d9cc69ca3b10d5f5",
"template": "billingo",
"signature": "9273882e8b3bc7f57e1ef3bc10041bc4bf9d835c152a1e0b810b77b3d51864ad",
"due_date": "2015-10-30",
...
...}
My WS Post handler method looks like this:
@POST
@Path("/invoice")
@Consumes("application/json")
@Produces("application/json")
public String createInvoice(CreateInvoiceBean newBillingoInvoice) {
LOG.info("invoicenum:. " + newBillingoInvoice.getDueDate());
return newBillingoInvoice.getDueDate();
}
My request arrives, and the createInvoice()
method invoked but if I call newBillingoInvoice.getDueDate()
it's return null, but when I call newBillingoInvoice.getSignature()
it's returning with the value that I sent in the request json.. And so on.. if I call newBillingoInvoice.getXY();
returns null
and if I call newBillingoInvoice.getOtherSomething();
return with value.. etc..
My question is, how could it happen that one property is null
and the other is not null
in the same object? When I create the request I set all properties no one of them was null
.
Please help me! Thank you!
It is due to the name I think. In your json we can see that you use underscore insead of upper case at word boundaries. Like due_date instead of dueDate. And I suppose that the properties in your code follow the usual java naming convetion with upper case.
One solution would be to annotate with @JsonProperty those set and get methods to change the name from "dueDate" to "due_date".
BTW the generated code is not for Genson, JsonPropertyOrder isn't a Genson annotation.