Параметру 2 конструктора в com.learningapp.backend.AcademixHub.services.UserService требуется bean-компонент типа com.learningapp .backend.AcademixHub.mappers.UserMapper', который не удалось найти.
Вот код UserService.java:
Код: Выделить всё
@Service
public class UserService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final UserMapper userMapper;
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, UserMapper userMapper) {
super();
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
this.userMapper = userMapper;
}
public UserDto register(SignUpDto signupDto) {
System.out.println("hello man hi hello");
Optional optionalUser = userRepository.findByEmail(signupDto.getEmail());
if (optionalUser.isPresent()) {
throw new AppException("Login already exists", HttpStatus.BAD_REQUEST);
}
User user = userMapper.signUpToUser(signupDto);
user.setPassword(passwordEncoder.encode(signupDto.getPassword()));
User savedUser = userRepository.save(user);
return userMapper.toUserDto(savedUser);
}
public UserDto login(LoginDto loginDto) {
User user = userRepository.findByEmail(loginDto.getEmail())
.orElseThrow(() -> new AppException("Unknown user", HttpStatus.NOT_FOUND));
if (passwordEncoder.matches(loginDto.getPassword(), user.getPassword())) {
return userMapper.toUserDto(user);
}
throw new AppException("Invalid password", HttpStatus.BAD_REQUEST);
}
public UserDto findByEmail(String login) {
User user = userRepository.findByEmail(login)
.orElseThrow(() -> new AppException("Unknown user", HttpStatus.NOT_FOUND));
return userMapper.toUserDto(user);
}
}
Код: Выделить всё
@Mapper(componentModel="spring")
@Component
public interface UserMapper {
UserDto toUserDto(User user);
@Mapping(target = "password", ignore = true)
User signUpToUser(SignUpDto signUpDto);
}
[Ошибка обработки запроса: java.lang.NullPointerException :
Невозможно вызвать «com.learningapp.backend.AcademixHub.repository.UserRepository.findByEmail(String)», поскольку «this.userRepository» имеет значение null] с основной причиной
Что мне следует сделать, чтобы это исправить?
Подробнее здесь: https://stackoverflow.com/questions/773 ... ould-not-b