2

I am using Spring Boot and lombok in my project and encounter some issues with it. My class looks like this:

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import lombok.Data;

@Data
@Document(collection = "elements")
public class ElementEntity {

    @Id
    private String id;
    // ...
}

Now, if I use jackson ObjectMapper to create my ElementEntity, I get the following runtime error:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of ElementEntity (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

But if I add the @NoArgsConstructor from lombok I get the following compilation error:

[ERROR] ElementEntity.java:[11,1] constructor ElementEntity() is already defined in class ElementEntity

It seems @Document adds one but probably only with package visibility. Is there an easy way to solve this, or I have to manually add a public no args constructor to every @Document?

Thomas
  • 6,325
  • 4
  • 30
  • 65

2 Answers2

5

Its a bug in lombok 1.16.22, try upgrading to 1.18.0,

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.0</version>
    <scope>provided</scope>
</dependency>

Read

benjamin c
  • 2,278
  • 15
  • 25
  • Thanks, I found this post just now as well, but didn't get that it is fixed in the later version of lombok. Using 1.18.2 now – Thomas Sep 06 '18 at 12:19
0

Try to change id field definition to this:

@Id
@Getter
@Setter
private String id;
roeygol
  • 4,908
  • 9
  • 51
  • 88
  • 1
    Hm, \@Data should take care of all the setter and getters. But it only adds the \@RequiredArgsConstructor but not the \@NoArgsConstructor which jackson seems to need. – Thomas Sep 06 '18 at 12:06