This error is coming while run on Maven install. can't find where the exact problem is.
I want run auth service but there is issue in the mapper, which is shows when I run maven install.
UserMapper. java Code, Where it shows error @Mapping
@Mapper
public interface UserMapper extends BaseMapper<User, UserDetailsDTO, Long> {
UserMapper MAPPER = Mappers.getMapper(UserMapper.class);
UserDTO userToUserDTO(User user);
User userDTOtoUser(UserDTO userDTO);
List<UserDTO> userToUserDTOList(List<User> users);
List<User> userDTOtoUserList(List<UserDTO> userDTOList);
@Mapping(source = "authorities", ignore = true, target = "authorities")
User toModel(UserDetailsDTO userDTO);
default Set<Role> convert123(Set<RoleDTO> roles ){
return roles.stream().map(DTO-> {
// Role role = new Role();
// role.setAuthority(DTO.getAuthority());
return RoleMapper.MAPPER.toModel(DTO);
}).collect(Collectors.toSet());
}
}
User.java Code
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@SuperBuilder
public class User extends BaseEntity<Long> {
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
@Column(nullable = false, columnDefinition = "boolean default true")
private boolean accountNonExpired = true;
@Column(nullable = false, columnDefinition = "boolean default true")
private boolean accountNonLocked = true;
@Column(nullable = false, columnDefinition = "boolean default true")
private boolean credentialsNonExpired = true;
@Column(nullable = false, columnDefinition = "boolean default true")
private boolean enabled = true;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
@JoinTable(
name = "user_role",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> authorities;
}
UserServices.java Code
@Service
public class UserService extends BaseService<User, UserDetailsDTO, Long> {
private final UserRepository repository;
private final UserMapper mapper;
private final UserDetailsService userDetailsService;
private final TokenGenerator tokenGenerator;
private final AuthenticationManager authenticationManager;
private final PasswordEncoder passwordEncoder;
public UserService(
UserRepository repository,
//UserMapper mapper,
UserDetailsService userDetailsService,
TokenGenerator tokenGenerator,
AuthenticationManager authenticationManager,
PasswordEncoder passwordEncoder) {
super(repository, UserMapper.MAPPER);
this.repository = repository;
this.mapper = UserMapper.MAPPER;
this.userDetailsService = userDetailsService;
this.tokenGenerator = tokenGenerator;
this.authenticationManager = authenticationManager;
this.passwordEncoder = passwordEncoder;
}
@Override
@Transactional
public UserDetailsDTO save(UserDetailsDTO userDetailsDTO) {
userDetailsDTO.setPassword(passwordEncoder.encode(userDetailsDTO.getPassword()));
//userDetailsDTO.setPassword(userDetailsDTO.getPassword());
userDetailsDTO.setAccountNonExpired(true);
userDetailsDTO.setAccountNonLocked(true);
userDetailsDTO.setCredentialsNonExpired(true);
userDetailsDTO.setEnabled(true);
return super.save(userDetailsDTO);
}
@Transactional
public UserDetailsDTO update(UserDetailsDTO userDetailsDTO) {
User existingUser =
repository
.findById(userDetailsDTO.getId())
.orElseThrow(() -> new NotFoundException("User not found"));
if (userDetailsDTO.getUsername() != null) {
existingUser.setUsername(userDetailsDTO.getUsername());
}
if (userDetailsDTO.getPassword() != null) {
existingUser.setPassword(passwordEncoder.encode(userDetailsDTO.getPassword()));
//existingUser.setPassword(userDetailsDTO.getPassword());
}
return this.mapper.toDTO(this.repository.save(existingUser));
}
public List<UserDTO> findByIdPublic(Set<Long> id) {
List<User> users = (List<User>) this.repository.findAllById(id);
if (users.isEmpty()) {
throw new NotFoundException("User id not found");
}
return this.mapper.userToUserDTOList(users);
}
public UserDetailsDTO findByUsername(String username) throws UsernameNotFoundException {
if (!getUsername().equals(username) && !hasAuthority(ROLE_ADMIN)) {
throw new ForbiddenException("You are not allowed to view this user's details");
}
return (UserDetailsDTO) userDetailsService.loadUserByUsername(username);
}
public Long findIdByUsername(String username) {
return this.repository
.findByUsername(username)
.orElseThrow(() -> new NotFoundException("Username not found"))
.getId();
}
public TokensDTO login(UserDTO userDTO) {
UsernamePasswordAuthenticationToken token =
new UsernamePasswordAuthenticationToken(
userDTO.getUsername(), userDTO.getPassword());
Authentication authentication = authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(authentication);
String username = userDTO.getUsername();
return new TokensDTO(
tokenGenerator.generateAccessToken(username),
tokenGenerator.generateRefreshToken(username));
}
public TokensDTO refresh(String refreshToken) {
refreshToken = refreshToken.substring(BEARER_PREFIX.length());
return new TokensDTO(tokenGenerator.refreshAccessToken(refreshToken), refreshToken);
}
}
These are the codes where the main functionality of authService is used, and the error can be there. Please help me to resolve this issue. First, after resolving every error, the code works perfectly fine, but after a few times, when I run it again, it shows this error. I had to run Maven clean and then Maven install.