Home > Enterprise >  UserRepo.findAll()" because "this.userRepo" is null in Spring Boot
UserRepo.findAll()" because "this.userRepo" is null in Spring Boot

Time:03-02

I am trying to develop a Spring Boot JWT authentication API. When the Spring Boot login page loads and the password is entered I get this error:

UserRepo.findAll()" because "this.userRepo" is null

How can this be fixed?

service class

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j

public class UserServiceImple implements UserService {

    private UserRepo userRepo;
    private  RoleRepo roleRepo ;
    @Override
    public User saveUser(User user) {
        // TODO Auto-generated method stub
        //log.info("Saving new user to the database",user.get);

        return userRepo.save(user);
    }

    @Override
    public Role saveRole(Role role) {
        // TODO Auto-generated method stub
        //log.info("Saving new Role {} to the database", role.getName());

        return roleRepo.save(role);
    }

    @Override
    public void addRoleToUser(String username, String roleName) {
        // TODO Auto-generated method stub
        //log.info("Adding role {} to user{}", roleName, username);
        User user = userRepo.findByUsername(username);
        Role role = roleRepo.findByName(roleName);
        user.getRoles().add(role);
        
    }

    @Override
    public User getUser(String username) {
        // TODO Auto-generated method stub
        //log.info("Fetching user {}", username);
        return userRepo.findByUsername(username);
    }

    @Override
    public List<User> getUsers() {
        // TODO Auto-generated method stub
        //log.info("Fetching all users");
        return userRepo.findAll();
    }

}

controller

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/")
public class userController {

    @Autowired
    private UserService userService;
    
    
    @GetMapping("/users")
    public ResponseEntity<List<User>>getUsers(){
        return ResponseEntity.ok().body(userService.getUsers());
    }
}

application.properties

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=my2022
logging.level.org.springframework.security=TRACE
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.formate_sql=true


server.error.include-message=always

CodePudding user response:

add this to your UserServiceImple class

@Autowired
public UserServiceImple(UserRepo userRepo,RoleRepo roleRepo){this.userRepo = userRepo;this.roleRepo = roleRepo;}

CodePudding user response:

The userRepo field is null because UserRepo (which I assume you have defined as a Spring bean) is not injected into the class. To fix this, you need to tell Spring to inject that bean.

There are several ways to inject the bean in Spring. The recommended approach is to use Constructor-based dependency injection.

To inject the beans in this manner, you need to have a constructor that takes in the beans to be injected. As you are using Lombok's @RequiredArgsConstructor annotation to generate a constructor from any final fields in the class, you need only make the fields final, and Lombok will generate the constructor you need:

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class UserServiceImple implements UserService {
    private final UserRepo userRepo;
    private final RoleRepo roleRepo;
    // ...
}

This will cause a constructor to be generated with signature public UserServiceImple(UserRepo userRepo, RoleRepo roleRepo), which will then be used by Spring to autowire those beans.

  • Related