mapstruct-找不到intellij的实现

5jvtdoz2  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(923)

使用最新的springboot mapstruct 1.3.1版本将DTO转换为对象(java 11,gradle 6.6版本)我得到以下异常:

org.springframework.beans.factory.UnsatisfiedDependencyException:
   Error creating bean with name 'userController' defined in 
   file [/directory/UserController.class]: Unsatisfied dependency expressed 
   through constructor parameter 0; nested exception is 
org.springframework.beans.factory.BeanCreationException:
   Error creating bean with name 'userServiceImpl' defined in 
   file [/directory/UserServiceImpl.class]: Bean instantiation via constructor failed; 
   nested exception is 
org.springframework.beans.BeanInstantiationException: 
  Failed to instantiate [package.service.UserServiceImpl]: 
  Constructor threw exception; 
  nested exception is 
java.lang.RuntimeException: java.lang.ClassNotFoundException: 
  Cannot find implementation for package.service.mapper.UserMapper

你能看看我哪里出错了吗?
我的代码:
构建.gradle

compile group: 'org.mapstruct', name: 'mapstruct', version: '1.3.1.Final'

用户控制器.java

@CrossOrigin(origins = "*", maxAge = 3306)
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {
    private final UserServiceImpl userService;

    @PostMapping
    public ResponseEntity<UserDTO> login(UserDTO request) {
            return ResponseEntity.ok(userService.login(request));
    }

用户服务.java

public interface UserService {
    public UserDTO login(UserDTO user);
}

userserviceimpl.java文件

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends CheckException implements UserService {
    private final UserRepository userRepository;
    private UserMapper mapper = Mappers.getMapper(UserMapper.class);

    @Override
    public UserDTO login(UserDTO user) {
        usernameParameterCheck(user.getId());
        User currentUser = userRepository.findUserByIdAndPassword(user.getId(), user.getPassword());
        userNullCheck(currentUser);
        return mapper.entityToDto(currentUser);
    }
}

用户Map器.java

@Mapper(componentModel = "spring")
public interface UserMapper {
    User dtoToEntity(UserDTO userDTO);
    UserDTO entityToDto(User user);
}
wswtfjt7

wswtfjt71#

我解决了这个问题:在dto类:d中添加'@noargsconstuctor'注解

相关问题