spring 使用MapStruct的Sping Boot 问题

k5ifujac  于 5个月前  发布在  Spring
关注(0)|答案(3)|浏览(53)

我是新来的Spring Boot 。我希望有人能帮助我了解我做错了什么。
我创造了服务。

@RequiredArgsConstructor 
@Service
public class UserService {
    
    private final UserRepository userRepository;

    private final UserMapper userMapper;

    public UserDto login(CredentialDto credentialDto) {
        User user = userRepository.findBylogin(credentialDto.login())
            .orElseThrow(()-> {
            ApplicationException ex =new ApplicationException();
            ex.setMessage("Unknown user");
            return ex;
        });
        return userMapper.toUserDto(user);
    }
}

字符串
我的Mapper类是:

@Mapper(componentModel = "spring")
public interface UserMapper {
    UserDto toUserDto(User user);
}


我在IDE中得到错误,如下所示:

说明:

Parameter 1 of constructor in com.example.jwttest.service.UserService required a bean of type 'com.example.jwttest.mappers.UserMapper' that could not be found.

操作:

Consider defining a bean of type 'com.example.jwttest.mappers.UserMapper' in your configuration.


非常感谢任何帮助。

vhmi4jdf

vhmi4jdf1#

可能你没有配置好:
除了依赖:

<dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>

字符串
你还需要配置maven-compiler-plugin

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>3.8.0</version>
     <configuration>
         <annotationProcessorPaths>
             <path>
                 <groupId>org.mapstruct</groupId>
                 <artifactId>mapstruct-processor</artifactId>
                 <version>${org.mapstruct.version}</version>
             </path>
             <path>
                 <groupId>org.projectlombok</groupId>
                 <artifactId>lombok</artifactId>
                 <version>${lombok.version}</version>
             </path>
             <dependency>
                 <groupId>org.projectlombok</groupId>
                 <artifactId>lombok-mapstruct-binding</artifactId>
                 <version>0.2.0</version>
             </dependency>
         </annotationProcessorPaths>
         <compilerArgs>
             <compilerArg>
                 -Amapstruct.defaultComponentModel=spring
             </compilerArg>
         </compilerArgs>
     </configuration>
</plugin>


查看此分步教程:springframework.guru/mapstruct

chy5wohz

chy5wohz2#

你可以看看我在GitHub上的迷你项目,它演示了MapStruct与Spring https://github.com/youssefehaab/java-mapstruct/tree/v1.0.0的集成。

kr98yfug

kr98yfug3#

如果你使用maven,也许你可以尝试先使用mvn clean命令进行清理。

相关问题