0

I'm getting the following error : Cannot resolve method 'setEmail' in 'User' in AuthService.java .

I try to add the constructor with parameters and it works. But why it doesn't work without ?

Here the classes :

AuthService.java

@Service
@AllArgsConstructor
@Slf4j
public class AuthService {

    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;

    @Transactional
    public void signup(RegisterRequest registerRequest) {
        User user = new User();
        user.setUsername(registerRequest.getUsername());
        user.setEmail(registerRequest.getEmail());
        user.setPassword(encodePassword(registerRequest.getPassword()));
        user.setCreated(now());
        user.setEnabled(false);

        userRepository.save(user);
    }

    private String encodePassword(String password) {
        return passwordEncoder.encode(password);
    }
}

User.java

@Data //responsable des getters et setters
@Entity
@Builder //Builder methods pour nos classes (Christophe nous a montré des exemples)
@AllArgsConstructor
@NoArgsConstructor
public class User {
    @Id
    @GeneratedValue(strategy = IDENTITY)
    private  Long userId;

    @NotBlank(message = "Le nom d'utilisateur est nécessaire")
    private String username;

    @NotBlank(message = "Le mot de passe est nécessaire")
    private String password;

    @Email
    @NotEmpty(message = "l'email est nécessaire")
    private String email;
    private Instant created;
    private boolean enabled;
}

UserRepository.java

import java.util.Optional;

public interface UserRepository extends JpaRepository<User, Long> {
    Optional<User> findByUsername(String username);
}
harili
  • 406
  • 9
  • 24
  • 1
    Which version of `lombok` you are using? And why do you need both `@Data` and `@Builder` both over an entity? – Kumar Ashutosh Dec 27 '20 at 21:08
  • @Ashutosh Well I'm using the lasted one (1.18. – harili Dec 27 '20 at 21:12
  • Well I'm using the lasted one (1.18.16). I need Data and Builder because I need my getter and setter ( – harili Dec 27 '20 at 21:14
  • Well, I'm not sure then what's the problem, but you should post your entire code repo. You may try several approach like, using builder pattern to create new User in service class, or remove `@Builder` and `@Data` and try only @Getter and @Setter in entity class. Also your field variables are final in service class but you have not initialized them in constructor. Please try these, Hope it helps – Kumar Ashutosh Dec 27 '20 at 21:15
  • I'd say it's the `@NotEmpty` annotation. What does it mean to initialize with null values (no args constructor) properties that cannot be null? – JulienD Dec 27 '20 at 21:19
  • @Ashutosh I didn't put that project in a repo because it's just a login test I want to make. I tried to remove Data and Builder from my class and only put ```Getter Setter``` but it doesn't work. Even in the AuthService class, when I try to call my getter setter, they doesn"t appear. (And I'm using the import of lombok is correctly done) – harili Dec 27 '20 at 22:00
  • @JulienD Hello, so no it's not that unfortunatelly – harili Dec 27 '20 at 22:00
  • Which IDE you are using? For lombok to work at runtime, you need to add the plugin to your IDE. Like for Intellij visit https://github.com/mplushnikov/lombok-intellij-plugin – Kumar Ashutosh Dec 27 '20 at 22:04
  • @Ashutosh I'm using IntelliJ – harili Dec 27 '20 at 22:12
  • 1
    Cool, please install the plugin in intellij, and verify in File>settings>plugins and in tools. Best of luck! – Kumar Ashutosh Dec 27 '20 at 22:13
  • 1
    @Ashutosh That's works ! Ty ! (you can put the answer) – harili Dec 27 '20 at 22:15

1 Answers1

3

Enabling the Lombok for runtime would solve the issue.

For IDEs to detect the lombok annotation at runtime, you need to add the integrate the lombok plugin.

Installing Lombok in Eclipse(Version: Photon Release (4.8.0))

  1. Download Lombok Jar File athttps://projectlombok.org/downloads/lombok.jar
  2. Start Lombok Installation Once the jar downloaded in Local repository, goto the jar location from command prompt and run the following command java -jar lombok-1.16.18.jar and we should be greeted by Lombok installation window provided by lombok like this.
  3. Provide eclipse installation path click on the “Specify Location” button and locate the eclipse.exe path under eclipse installation folder
  4. Finally install this by clicking the “Install/Update” and then click quit installer
  5. Restart the eclipse IDE. It will perform some background process and errors should be removed.

Installing Lombok in Intellij(Version: Ultimate 2018.2)

  1. Go to File > settings > plugins.
  2. Type lombok in search box provided(version : 0.22.2.08.2 or higher)
  3. Or visithttps://github.com/mplushnikov/lombok-intellij-plugin
  4. Restart the IDE.

Related issues:

Can't compile project when I'm using Lombok under IntelliJ IDEA

Kumar Ashutosh
  • 1,121
  • 10
  • 33