gpt4 book ai didi

java.lang.ClassNotFoundException : Cannot find implementation for com. 如何处理java.demo.mapper.MapperDTO

转载 作者:行者123 更新时间:2023-12-01 18:15:34 67 4
gpt4 key购买 nike

我正在使用 API Rest 构建一个 CRUD 应用程序,我将使用 Mapstruct 库,但我不能。当我尝试使用自定义对象映射通过读取数据库返回的数据时,出现以下错误

你能帮我解决这个问题吗?我收到以下错误

java.lang.ClassNotFoundException: Cannot find implementation for com.howtodoinjava.demo.mapper.MapperDTO
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:75)
at org.mapstruct.factory.Mappers.getMapper(Mappers.java:58)
at com.howtodoinjava.demo.mapper.MapperDTO.<clinit>(MapperDTO.java:14)
at com.howtodoinjava.demo.service.CustomerServiceImpl.getCustomerById(CustomerServiceImpl.java:35)
at com.howtodoinjava.demo.controller.CutomerController.getUtentiEntityById(CutomerController.java:74)

我的文件

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

    @Override
public Customer getCustomerById(Integer id) throws RecordNotFoundException {
Optional<CustomerDTO> utentiEntity = repository.findById(id);
try {
if (utentiEntity.isPresent()) {
return MapperDTO.INSTANCE.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"})

public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}

最佳答案

MapStruct 是一个代码生成器。由于某种原因,您的代码未生成。检查 Maven 构建日志以查看那里发生了什么。

此外,您可以删除:

            <dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.3.1.Final</version>
</dependency>

来自您的pom.xml

关于java.lang.ClassNotFoundException : Cannot find implementation for com. 如何处理java.demo.mapper.MapperDTO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60377689/

67 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com