2

I have a spring boot app 2.0.5.RELEASE with a lombok dependency for version 1.18.2 with scope set to provided.

An exmaple:

@RestController
@RequestMapping("/users")
@AllArgsConstructor
public class UserController {

    private static final UserMapper mapper = Mappers.getMapper(UserMapper.class);

    private UserRepository repository;//It's null, nothing gets injected

    @GetMapping("/")
    public ResponseEntity<List<UserDTO>> getUsers() {

        final List<User> users = (List<User>) repository.findAll();

        return new ResponseEntity<>(users.stream()
                .map(mapper::toDto)
                .collect(Collectors.toList()), HttpStatus.OK);
    }
}

In that case I'm getting an error as repository field is null. When I remove lombok @AllArgsConstructor and put it directly:

public UserController(UserRepository repository) {
    this.repository = repository;
}

Then it works, a proper component is injected in the repository field. The same situation is for UserDTO class. It's definied:

@Getter @Setter
public class UserDTO {

    private int id;
    private String firstName;
    private String lastName;
}

Jackson is not able to find getters and throws an exception. Everything works fine if getters are created "normally" (without 3rd party libs).

What am I doing wrong? Why lombok is not generating things it should?

Mikhail Kholodkov
  • 23,642
  • 17
  • 61
  • 78
user3529850
  • 1,632
  • 5
  • 32
  • 51
  • 1
    For Jackson to work, you also need to add `@NoArgsConstructor` – syntagma Oct 16 '18 at 15:16
  • And that works without @Autowired on constructor?? Right it works since 4.3 https://stackoverflow.com/a/49893384/1527544 – Antoniossss Oct 16 '18 at 15:17
  • 1
    It's not working when you are building app with IDE or Maven/Gradle? If IDE Lombok plugin should be installed – Alexander Terekhov Oct 16 '18 at 15:17
  • 2
    Why do you set scope to "provided"? IMO it shoud be "compile". – Paweł Adamski Oct 16 '18 at 15:18
  • @PawełAdamski should not - you dont want to include lombok into resulting jar, you only want it on the classpath during compilation. – Antoniossss Oct 16 '18 at 15:19
  • 1.) shouldn't be to compile https://stackoverflow.com/questions/29385921/maven-scope-for-lombok-compile-vs-provided 2.) I have plugin installed in my Intellij Idea IDE 3.) No need for `Autowired` annotation in this version of spring – user3529850 Oct 16 '18 at 15:21
  • @user3529850 that post says it should be *provided*. Probably you wanted to write "should not" – Antoniossss Oct 16 '18 at 15:21
  • 1
    If setters are not visible through IDE, it seems that annotation processing is not enabled: https://stackoverflow.com/questions/24006937/lombok-annotations-do-not-compile-under-intellij-idea Or try to build app using maven or gradle command line – Alexander Terekhov Oct 16 '18 at 15:24
  • @user3529850 as for lombok constructors with DI check this https://www.baeldung.com/spring-injection-lombok. – Antoniossss Oct 16 '18 at 15:27

3 Answers3

4

I fixed it by ticking the "Enable annotation processing" checkbox in Settings->Compiler->Annotation Processors.

GauravRai1512
  • 834
  • 6
  • 14
  • Worked. Thanks. I also had to do one moer thing: move project from `Maven default annotation processors` to `Default` – user3529850 Oct 16 '18 at 16:02
1

Apart from other answers to enable annotation processing in Intellij IDE, we need to add a plugin to make the IDE understand Lombok Project. Install the Lombok plugin: Settings > Plugins > Lombok.

SwarupCode
  • 11
  • 1
0

Settings->Build/Execution/Deployment - > Compiler->Annotation Processors -> Enable Annotation Processor ckeck

hbib
  • 1