0

This is my class

@Builder
@Value
public class A {
    int id;
    String name;
    @NonNull String lastName;
}

As you know, Builder will add the all args constructor.

I need to deserialise a string into a POJO object.

I added my mixing like this:

public abstract class AMixin {
    public AMixin(@JsonProperty("name") String name,
                  @JsonProperty("id") int id,
                  @JsonProperty("lastName") String lastName) {
    }

    @JsonProperty("name")
    abstract String getName();

    @JsonProperty("id")
    abstract int getId();

    @JsonProperty("lastName")
    abstract String getLastName();

}

As you can see, all three properties are there.

I deserialise like this:

public static void main(String[] args) throws Exception {


        ObjectMapper mapper = new ObjectMapper();
        mapper.addMixIn(A.class, AMixin.class);

        String ss = "{\"id\":1,\"name\":\"some name\",\"lastName\":\"some name\"}\n";
        A c = mapper.readValue(ss, A.class);
    }

but I get this error:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.bla.test.A` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (String)"{"id":1,"name":"some name","lastName":"some name"}
"; line: 1, column: 2]
Mario Galic
  • 41,266
  • 6
  • 39
  • 76
Marco Dinatsoli
  • 9,244
  • 33
  • 108
  • 224
  • Did you try adding @NoArgsConstructor ? If it does not work check this one https://stackoverflow.com/questions/51464720/lombok-1-18-0-and-jackson-2-9-6-not-working-together – Traycho Ivanov Aug 07 '20 at 21:29
  • @TraychoIvanov I don't want the no args constructor. My class is final and all its fields are final and must be declared in the initiation phase (constructor) – Marco Dinatsoli Aug 07 '20 at 21:49
  • In this case you need this approach with `@JsonDeserialize` , here there is a good example https://stackoverflow.com/a/48801237/9671280 – Traycho Ivanov Aug 07 '20 at 21:55
  • @TraychoIvanov the whole idea of mixin is to avoid adding Jackson annotation in your POJO so I'd prefer to know why my mix is not working. – Marco Dinatsoli Aug 07 '20 at 22:07
  • @Marcoo this is pure lombok issue, believe me or not the once chance to solve it by testing the latest jackson versions otherwise you need to accept the compromise. – Traycho Ivanov Aug 07 '20 at 22:19
  • @TraychoIvanov I found the solution :) (no compromise on POJO being clean) – Marco Dinatsoli Aug 07 '20 at 22:27
  • 1
    Does this answer your question? [Lombok 1.18.0 and Jackson 2.9.6 not working together](https://stackoverflow.com/questions/51464720/lombok-1-18-0-and-jackson-2-9-6-not-working-together) – Traycho Ivanov Aug 07 '20 at 22:30

1 Answers1

0

I found the answer.

Add lombok.config file with content:

lombok.anyConstructor.addConstructorProperties=true
Marco Dinatsoli
  • 9,244
  • 33
  • 108
  • 224