我正在使用api rest构建一个原始应用程序,而我将使用mapstruct库,但我不能。当我尝试使用自定义对象映射通过读取数据库返回的数据时,出现以下错误
您能帮我解决这个问题吗?我收到以下错误
申请无法开始
说明:
无法找到com.howtodoinjava.demo.service.CustomerServiceImpl中的字段mapperDTO的类型为com.howtodoinjava.demo.mapper.MapperDTO的bean。
注入点具有以下注释:-@ org.springframework.beans.factory.annotation.Autowired(required = true)
动作:
考虑在您的配置中定义类型为'com.howtodoinjava.demo.mapper.MapperDTO'的bean。
我的文件
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 http://maven.apache.org/xsd/maven-
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.howtodoinjava</groupId>
<artifactId>demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
<org.projectlombok.version>1.18.10</org.projectlombok.version>
<m2e.apt.activation>jdt_apt</m2e.apt.activation>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency> -->
<!-- <groupId>com.jayway.jsonpath</groupId> -->
<!-- <artifactId>json-path</artifactId> -->
<!-- <scope>test</scope> -->
<!-- </dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>jcc</artifactId>
<version>11.5.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- Map strunct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.3.1.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-jdk8 -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.1.Final</version>
</dependency>
<!-- json logback -->
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-json-classic</artifactId>
<version>0.1.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-jackson</artifactId>
<version>0.1.5</version>
</dependency>
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Utility -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.bwaldvogel</groupId>
<artifactId>mongo-java-server</artifactId>
<version>1.16.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<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.3.1.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${org.projectlombok.version}</version>
</path>
</annotationProcessorPaths>
<!-- <compilerArgs> -->
<!-- <arg>-Amapstruct.defaultComponentModel=spring</arg> -->
<!-- </compilerArgs> -->
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
MapperDTO.java
@Mapper(componentModel="spring")
/*Durante la compilazione, MapStruct genererà un'implementazione di questa interfaccia.*/
public interface MapperDTO {
//MapperDTO INSTANCE = Mappers.getMapper(MapperDTO.class);
Customer customerDTOtoCustomer(CustomerDTO customerDTO);
CustomerDTO customertoCustomerDTO(Customer customer);
}
ServiceImpl.java
@Autowired
private MapperDTO mapperDTO;
@Override
public Customer getCustomerById(Integer id) throws RecordNotFoundException {
Optional<CustomerDTO> utentiEntity = repository.findById(id);
try {
if (utentiEntity.isPresent()) {
return mapperDTO.customerDTOtoCustomer(utentiEntity.get());
} else {
throw new RecordNotFoundException("No entity record exist for given id");
}
}
catch (Exception e ) {
log.error(" message " +e.getMessage(),e);
}
return null;
}
demoapplication.java
@SpringBootApplication(scanBasePackages="com.howtodoinjava.demo")
@EnableJpaRepositories (basePackages="com.howtodoinjava.demo.repository")
@EntityScan(basePackages="com.howtodoinjava.demo.modelDTO")
@ComponentScan({"com.howtodoinjava.demo.service","com.howtodoinjava.demo.controller", "com.howtodoinjava.demo.mapper"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
我认为mapstruct jar不正确,否则它将在运行时动态地将实体转换为dto并将dto转换为实体。但是,如果您不想使用mapstruct,则可以通过提供这样的方法实现来手动进行转换。
@Override
public ProductDTO toProductDTO(Products products) {
if (products == null) {
return null;
}
ProductDTO productDTO = new ProductDTO();
productDTO.setName(products.getName());
productDTO.setDescription(products.getDescription());
productDTO.setPrice(products.getPrice());
return productDTO;
}
@Override
public List<ProductDTO> toProductDTOs(List<Products> products) {
if (products == null) {
return null;
}
List<ProductDTO> listproductdto = new ArrayList<ProductDTO>();
for (Products eachproducts : products) {
listproductdto.add(toProductDTO(eachproducts));
}
return listproductdto;
}
@Override
public Products toProduct(ProductDTO productDTO) {
// TODO Auto-generated method stub
if (productDTO == null) {
return null;
}
Products products = new Products();
products.setName(productDTO.getName());
products.setPrice(productDTO.getPrice());
products.setDescription(productDTO.getDescription());
return products;
}
有关更多详细信息,您可以检查我的示例,我也尝试过相同的示例。从这里enter link description here
使用
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.3.1.Final</version>
</dependency>
并且在编译时,请检查地图结构是否在目标中生成了MapperDTOImpl类