简体   繁体   中英

Jersey + Gson not deserializing java.util.Date

I'm having a strange issue with a little servlet which uses Jersey and Gson for the JSON serialization/deserialization. I actually copy-pasted the basic Gson provider written for Jersey, like this one: http://eclipsesource.com/blogs/2012/11/02/integrating-gson-into-a-jax-rs-based-application/ and everything seemed to work fine, until I tried to deserialize a Date (in the standard ISO 8601 format), which always gets mapped into my POJO as null.

My first try was to register a deserializer type adapter before returning the gsonBuilder instance, like that:

import java.util.Date;

...

gsonBuilder.registerTypeAdapter(Date.class,
    new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type type,
                                JsonDeserializationContext arg2) throws JsonParseException {
            try {
                System.out.println(json);
                return (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXX")).parse(json.getAsString());
            } catch (ParseException e) {
                return null;
            }
        }
    });

This didn't work, and nothing is printed out when I send the POST request. I tried to use the setDateFormat method on the gsonBuilder instance before returning it, but this didn't change anything.

I thought there were some others classes implementing the MessageBodyWriter and MessageBodyReader overriding my own implementation, so I tried to delete my own implementation and Jersey complained that it wasn't able to deserialize the JSON (so there are no other providers, i guess).

I tried to set breakpoints in the readFrom method in my MessageBodyReader but the request is actually deserialized without suspending the execution.

I should mention that my class contains different fields too, some strings and one date: the string are always deserialized correctly.

I tried sending different dates, starting with 2016-06-23T00:00:00.000+0200 (which should be formatted with the date format string I used in the code above), and getting to the simple 2016-06-17 by removing one part at the time, and it never worked.

I cleaned my maven project, recompiled it and it didn't work.

I thought it could have been Jetty not loading the correct classes, so i deployed the same code into a Tomcat 8 server, and the result was the same.

My last try was to write another parallel MessageBodyReader but instead of making it generic for the Object type, I made a specific java.util.Date deserializer, and still the readFrom method seems not to be called.

I seriously don't know what I could try now, do you have any idea?

Thanks in advance.

The reason of the error is here...

 try {
     System.out.println(json);
     return (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXX")).parse(json.getAsString());
 } catch (ParseException e) {
     return null;
 }

to be more specific here:

"yyyy-MM-dd'T'HH:mm:ss.SSSXX"

java Date and SimpleParser can hold at the most only 3 places for the milliseconds, and there is no wildcard .SSS XX in the string used for the SimpleDateFormat, so your parser is throwing an Exception, that you are catching but returning a Date referenced to null, Ergo:

Gson is handling a null referenced object,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM