0

I am making a Java EE 7 application (war) on Glassfish 4.1.
The application is a simple CRUD app.
For testing CRUD operations I have made a Java SE Client (Using Jersey2 client api). The strange behaviour is that the HTTP POST Creation does not detect one field of the object:
I have a "birthDate" attribute, which comes "null" always in the server side.
I dont understand why this is happens and how to resolve this?

Here is the dependencies that I am using in the server-side:

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.core</artifactId>
        <version>2.6.0</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>

Here is the client side dependencies:

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.22</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.6.2</version>
    </dependency>

Here is the creation block in client side:

    Client client = ClientBuilder.newClient();
    WebTarget webTarget = client.target("http://localhost:8080/MedicalCenter/rest/customers");

    Customer customer = new Customer("Jason", "Bourne", new Date(), "This is description", new ArrayList<Device>());

    ObjectMapper mapper = new ObjectMapper();

    String mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    result = mapper.writeValueAsString(customer);

    Response response = webTarget.request("application/json")
            .post(Entity.entity(result, MediaType.APPLICATION_JSON_TYPE));

Here is an example of a sent JSON:

Here is an example {"id":null,
"name":"Jason",
"familyName":"Bourne",
"birthDate":"2015-10-05T21:41:43‌​.044+0000",
"description":"This is description",
"creation":"2015-10-05T21:41:43.044+0000",
"enabled":true,
"system":t‌​rue,
"version":null,
"devices":[]}

Please help me :) Thank you !!

Jason Bourne
  • 756
  • 1
  • 14
  • 34

1 Answers1

0

The situation got more stranger when the http post creation has passed with Jersey 1.19 client bundle.
So I got a confirmation that the hypothesis saying that it's a deserialization problem in the server side is incorrect, as it works with the jersey 1.19 client bundle.
I re-started the investigation and I compared the JSON output of both Jersey 1 and Jersey 2 client, and I found that the difference is the DateFormat of the birthDate:

  • Jeysey 2: "birthDate":"1990-05-04T22:00:00.000+0000"
  • Jersey 1: "birthDate":"2015-10-06T00:24:32.693+02:00" <-- This is working

So I tried to force the DateFormat in the Jersey 2 client:

DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
//Jackson Object Mapper
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(sdf);

And this is working 5/5

The next question that invoke this workaround, why the only accepted format is this?

Hope this workaround will help everyone who has the same issue !

Jason Bourne
  • 756
  • 1
  • 14
  • 34
  • 1
    The one thing to consider is that the actual default JSON provider used in Glassfish is MOXy and not Jackson, as mentioned [here](http://stackoverflow.com/a/29247558/2587435). – Paul Samsotha Oct 06 '15 at 02:43