0

I am building an API using java and the following libraries

  • javax.ws.rs-api-2.0.1
  • genson-1.4
  • joda-time-2.9.6

I am now trying to create a post request with a list of object as a parameter

@Path("service")
public class ServiceResource {

    @Path("/stations/flow/forecasts")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public List<Station> getStations(List<Station> stations) {
            //Code
    }
}

This class Station contains a list of another class which has a DateTime variable.

The data are sent from a web app using javascript. It is working fine when I am not sending the nested list with the datetime object. When I try to send it I receive the error :

java.lang.InstantiationException

The json sent looks like this

[
   {
      "code":"1048",
      "measures":[
         {
            "code":"10481002",
            "observations":[
               {
                  "date":{
                     "chronology":{
                     },
                     "millis":1487322000000
                  },
                  "value":1.702
               }
            ]
         }
      ]
   }
]

And the java class linked to this date and value

public class Value {
    private DateTime date;
    private Float value;
}

How is the code not working when sending the DateTime ?

Note: Everything else is working fine in the API : posting an user object, get data containing datetime, sending back datetime

EDIT

I found out that there is an extension for joda-time. I added the following code

@Provider
public class GensonProvider implements ContextResolver<Genson> {
    private final Genson genson = new GensonBuilder().useConstructorWithArguments(true).useDateAsTimestamp(false).withBundle(new JodaTimeBundle()).create();

    @Override
    public Genson getContext(Class<?> type) {
        return genson;
    }
}

But it still gives me the same result

Weedoze
  • 13,683
  • 1
  • 33
  • 63
  • Do you use JDK 8? – m.genova May 04 '17 at 12:02
  • @m.genova I have jdk 1.8 yes – Weedoze May 04 '17 at 12:04
  • You could you use new time and date java api (see http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html) inspired by joda time, probably you don't suffer this problem. – m.genova May 04 '17 at 12:10
  • @m.genova It would be really complicated to replace all the `DateTime` inside the whole project – Weedoze May 04 '17 at 12:14
  • 1
    You should use Jackson. It will be able to handle JodaTime (with a little tweaking) – Paul Samsotha May 04 '17 at 12:21
  • @peeskillet What do you mean by a little tweaking ? – Weedoze May 04 '17 at 12:28
  • @peeskillet Is there a way using genson ? – Weedoze May 04 '17 at 12:37
  • _"What do you mean by a little tweaking ?"_ - [Configure the ObjectMapper](http://stackoverflow.com/questions/28307646/how-to-configure-jackson-in-wildfly/28310779#28310779) with the [`JodaModule`](https://github.com/FasterXML/jackson-datatype-joda). _"Is there a way using genson ?"_ - I don't know. I don't use Genson – Paul Samsotha May 04 '17 at 12:43

1 Answers1

0

Your date format is not standard. Change this

"date":{
  "chronology":{
  },
  "millis":1487322000000
}

to "date": 1487322000000 or to a string formatted date (in which case you will want to configure Genson to parse your format).

eugen
  • 5,856
  • 2
  • 29
  • 26
  • How can I change it ? This is automatically generated – Weedoze May 13 '17 at 10:49
  • @Weedoze so this generated json comes from some javascript client and you don't have control over it? Genson has no way of knowing what this format is as it's not a standard representation of date times. In this case you will have to implement a custom DateTime converter that is able to handle this structure see http://owlike.github.io/genson/Documentation/UserGuide/#custom-serde – eugen May 15 '17 at 18:58
  • This json format comes first from the server. I would like to send it back to server. This is not something generated from client-side. I have already tried [Genson genson = new GensonBuilder().withBundle(new JodaTimeBundle()).create();](http://owlike.github.io/genson/Documentation/Extensions/) – Weedoze May 16 '17 at 06:11
  • Perhaps the server is not picking your custom genson instance. You should try to see if things would correctly work from a plain main method (so serialize with Genson your object and then read it back). If this works then the problem is that you need to tell your JAX-RS implementation where to find GensonProvider. – eugen May 16 '17 at 19:47