Deserialization of Class with optional Property (Dropwizard 0.8-SNAPSHOT, Jersey 2, Jackson 2.4, Java 7)

607 views
Skip to first unread message

kile...@gmail.com

unread,
Sep 23, 2014, 2:20:49 AM9/23/14
to dropwiz...@googlegroups.com
I have the following value object class

public class Data {

    private Long id;

    public Data() {
    }

    public Data(final Long id) {
        this.id = id;
    }

    public Optional<Long> getId() {
        return Optional.fromNullable(this.id);
    }

    public void setId(final Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() { ... }

    @Override
    public boolean equals(final Object obj) { ... }

    @Override
    public String toString() { ... }
}

The id property being optional (available for values that are already stored in the DB but not for new ones).

When I try to post (or put) such data, Jackson throws an error:

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of long out of START_OBJECT token
 at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@1b0e0936; line: 1, column: 2] (through reference chain: com.acomplii.rs.Data["id"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:749)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseLong(StdDeserializer.java:460)

This is because the getter for id is optional (an Object) while the setter itself is long (a scalar value).

This code was working through out the 0.7.x series and early 0.8-SNAPSHOTs before the upgrade to Jersey 2 (and Jackson 2.4 I think).

Tatu Saloranta

unread,
Sep 23, 2014, 11:22:19 AM9/23/14
to dropwiz...@googlegroups.com
Is this Optional from Guava, or JDK8? For former to work, you need to add guava datatype module (jackson-datatype-guava). Latter is not yet supported, although with jdk8 datatypes module it should be difficult to add support for.

Or, you can add custom deserializer for it as well.

-+ Tatu +-

--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kile...@gmail.com

unread,
Sep 27, 2014, 10:20:47 AM9/27/14
to dropwiz...@googlegroups.com
Hi Tatu.

It's Guava Optional (sorry if it wasn't clean from Java 7 in the heading there).

It's my understanding Dropwizard includes support for guava types by default (or at least it used to). I'll just double check.

I've ended up having a custom deserializer but I don't like having Optional<Type>Serializer for each type that I end up using with Optional.

Thanks.

Tatu Saloranta

unread,
Sep 27, 2014, 12:58:57 PM9/27/14
to dropwiz...@googlegroups.com
It could also be a bug that has been fixed in a later version of Guava datatype module. But Optional is definitely supported by the module, and I think you are right in that it is also included by DW by default.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages