我正在使用带有mapstruct的Spring Boot,每次重新启动计算机时,当我尝试启动我的应用程序时,我都会收到此错误:
Description:
Field personMapper in com.paulo.enterprise.credito.services.PersonServices required a bean of type 'com.paulo.enterprise.credito.mapper.PersonMapper' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.paulo.enterprise.credito.mapper.PersonMapper' in your configuration.
我可以修复错误的唯一方法是在终端中运行 mvn cleancompile,之后我可以根据需要多次停止和启动应用程序,并且在我关闭计算机并打开后,错误将不会再次显示再次打开并尝试运行我的应用程序,错误返回,我需要再次运行 mvn clean 编译。问题出在mapstruct包中,但我不知道如何修复它。(我使用postgresql作为我的Docker数据库,我使用eclipse IDE)
My Person(它有 getter、setter、hashcode 和 equals):
@Entity
@Table(name = "person")
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name", nullable = false, length = 255)
private String firstName;
@Column(name = "last_name", nullable = false, length = 255)
private String lastName;
@Column(nullable = false, length = 255)
private String address;
@Column(nullable = false, length = 6)
private String gender;
My PersonDTO(它有 getter、setter、hashcode 和 equals):
@JsonPropertyOrder({ "id", "gender", "firstName", "lastName", "address" })
public class PersonDTO implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String firstName;
private String lastName;
private String address;
private String gender;
public PersonDTO() {}
我的人物映射器:
package com.paulo.enterprise.credito.mapper;
@Mapper(componentModel = "spring")
public interface PersonMapper {
@Mapping(target = "firstName", source = "person.firstName")
PersonDTO personToPersonDTO(Person person);
List<PersonDTO> personListToPersonDTOList(List<Person> persons);
Person personDTOToPerson(PersonDTO personDTO);
}
My PersonService(它具有所有 CRUD 方法):
@Service
public class PersonServices {
private Logger logger = Logger.getLogger(PersonServices.class.getName());
@Autowired
PersonRepository repository;
@Autowired
private PersonMapper personMapper;
@Autowired
private PersonMapperV2 personMapperV2;
public List<PersonDTO> findAll() {
logger.info("Finding all persons");
return personMapper.personListToPersonDTOList(repository.findAll());
}
public PersonDTO findById(Long id) {
logger.info("Finding one person");
Person person = repository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("No records found for this id"));
return personMapper.personToPersonDTO(person);
}
public PersonDTO create(PersonDTO person) {
logger.info("Creating one person");
Person personEntity = personMapper.personDTOToPerson(person);
Person savedPerson = repository.save(personEntity);
return personMapper.personToPersonDTO(savedPerson);
}
我的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>3.2.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.paulo.enterprise.credito</groupId>
<artifactId>myapp-0.1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myapp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>21</java.version>
<org.mapstruct.version>1.5.5.Final</org.mapstruct.version>
</properties>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
查看您的 pom.xml,您似乎尚未配置 Eclipse 来正确配置您的项目。
您需要确保安装了 m2e-apt 插件(如果您使用的是 Eclipse 2022.9 或更高版本,它会自动安装)。
您应该确保您的房产中有以下内容
<!-- automatically run annotation processors within the incremental compilation -->
<m2e.apt.activation>jdt_apt</m2e.apt.activation>
MapStruct 网站上的Eclipse IDE 设置部分也对此进行了解释