Lombok + Jackson + Inheritance + Immutables deserialization error

2,731 views
Skip to first unread message

Daniel

unread,
Apr 9, 2018, 7:54:25 PM4/9/18
to Project Lombok
Hey,
I'm trying Lombok for the first time and I'm very happy. Thanks for saving my precious time! :)
I do have one problem though, I can't figure out how to annotate this properly:

- both classes should have builder
- all fields should be final

@Data
@AllArgsConstructor
@EqualsAndHashCode
public class Coordinate {

    protected final String id;
    protected final Double x;
    protected final Double y;
}

@Data
@EqualsAndHashCode(callSuper = true)
public class RoutableCoordinate extends Coordinate {

    @JsonProperty("routingSpec")
    private final RoutingSpecification routingSpecification;

    @Builder
    public RoutableCoordinate(String id, Double x, Double y, RoutingSpecification routingSpecification) {
        super(id, x, y);
        this.routingSpecification = routingSpecification;
    }
}

This results in "Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `net.motionintelligence.commons.api.geo.RoutableCoordinate` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)" during Deserialization? Not quite sure if this is a jackson or lombok or my problem? Also I'm using lombok 1.16.18.

Cheers,
Daniel

Martin Grajcar

unread,
Apr 9, 2018, 10:11:33 PM4/9/18
to project...@googlegroups.com
I'm just guessing:

Adding a dummy default constructor and letting Jackson set the final fields despite final via reflection might work.
I guess, @NoArgsConstructor(force=true) is such a constructor.

Jackson can use a constructor with @ConstructorProperties, but they don't get generated automatically anymore:

  • BREAKING CHANGE: lombok config key lombok.anyConstructor.suppressConstructorProperties is now deprecated and defaults to true, that is, by default lombok no longer automatically generates @ConstructorProperties annotations. New config key lombok.anyConstructor.addConstructorPropertiesnow exists; set it to true if you want the old behavior. Oracle more or less broke this annotation with the release of JDK9, necessitating this breaking change.

The annotation @JsonProperty("routingSpec") won't get copied to the constructor arguments.
This means that the dummy default constructor with reflection can work, but not the @AllArgsConstructor (unless you use the same name both in Java and JSON).

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

Reply all
Reply to author
Forward
0 new messages