SpringBoot 无法自动装配。没有发现“UserMapper”类型的 Mapstruct Mappers 错误

问题描述 投票:0回答:2

这是我的用户类。

@Entity
@Table(name = "user_table")
@Data
public class User {

@Id
private Long id;
private String userName;
private String password;
 }

这是我的 UserDto,我知道字段名称是相同的。但我正在尝试理解这个概念。

@Data
public class UserDto {

private Long id;
private String userName;
private String password;
}

我想使用 MapStruct 进行 Entity-Dto 和 Dto-Entity 转换。所以我写了一个像 EntityMapper 这样的基本接口。

public interface EntityMapper<D, E> {

E toEntity(D dto);

D toDto(E entity);

List<E> toEntity(List<D> dtoList);

List<D> toDto(List<E> entityList);

}

然后我创建了一个新的接口,它扩展了 EntityMapper .

@Mapper(componentModel = "spring", uses = UserService.class)

public interface UserMapper extends EntityMapper<UserDto , User> {


@Mapping(source = "id", target = "id")
@Mapping(source = "userName", target = "userName")
@Mapping(source = "password", target = "password")
@Override
User toEntity(UserDto dto);

@Override
UserDto toDto(User entity);

@Override
List<User> toEntity(List<UserDto> dtoList);

@Override
List<UserDto> toDto(List<User> entityList);

这是我的控制器

@RestController
@RequestMapping("/v1/api/users")
public class UserController {

private final UserService userService;

public UserController(UserService userService) {
    this.userService = userService;
}

@PostMapping("/save")
public ResponseEntity<UserDto> saveUser(@RequestBody UserDto userDto){

    UserDto res = userService.saveUser(userDto);
    return ResponseEntity.ok(res);
}
}

最后,当我尝试创建一个新的服务类并使用 UserMapper 接口的引用时。春天告诉我

无法自动装配。未找到“UserMapper”类型的 bean。

对于这条线

   private final UserMapper userMapper;

这就是我的整个服务级别。当我删除 @Service 注释时,错误已修复。但这是一个服务类,所以我认为我必须添加 @Service 注释。

我尝试向 UserMapper 添加 @Component 注释。但应用程序无法启动。

@Service
public class UserService {

private final UserRepository userRepository;
private final UserMapper userMapper;

public UserService(UserRepository userRepository, UserMapper userMapper) {
    this.userRepository = userRepository;
    this.userMapper = userMapper;
}

public UserDto saveUser(UserDto userDto){

    User user = userMapper.toEntity(userDto);
    user = userRepository.save(user);
    UserDto res = userMapper.toDto(user);
    return res;
}

}

这是 pom.xml。

  <?xml version="1.0" encoding="UTF-8"?>
   <project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
      https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.6</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>AprilSevenApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>AprilSevenApp</name>
<description>AprilSevenApp</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -->
    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>1.4.2.Final</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.dot</groupId>
        <artifactId>util</artifactId>
        <version>0.2.0</version>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

我刚刚添加了映射结构的依赖关系。如果我必须添加不同的依赖项,我不知道。

<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -- 
 >
<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.4.2.Final</version>
</dependency>
java spring-boot autowired dto mapstruct
2个回答
4
投票

必须将注释处理器添加到编译器插件中才能生成

UserMapper
接口的实现。

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <source>1.8</source>
    <target>1.8</target>
    <annotationProcessorPaths>
        <path>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-processor</artifactId>
            <version>1.4.2.Final</version>
        </path>
    </annotationProcessorPaths>
  </configuration>
</plugin>

此外,考虑到

uses = UserService.class
不是映射器,我认为不能删除
UserService
。这是
uses
注解参数的官方文档:

此映射器使用的其他映射器类型。可能是手写的类或由 MapStruct 生成的其他映射器。生成的映射器类之间不必创建循环。


0
投票

添加到Mapper注释中的一段代码帮助我解决了这个问题

@Mapper(componentModel = "spring")
© www.soinside.com 2019 - 2024. All rights reserved.