java.lang.ClassNotFoundException:无法找到com. howtodoinjava. demo. mapper. MapperDTO的实现

xqk2d5yq  于 2023-02-18  发布在  Java
关注(0)|答案(2)|浏览(300)

我正在使用API rest构建一个crud应用程序,并且打算使用mapstruct库,但是我不能。当我尝试Map通过阅读数据库返回的数据时,我得到了以下错误
你能帮我解决这个问题吗?我得到以下错误

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);
    }

}
mqkwyuun

mqkwyuun1#

MapStruct是一个代码生成器。由于某些原因,您的代码没有生成。检查maven构建日志,看看那里发生了什么。
此外,您还可以删除:

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

从您的pom.xml

gmxoilav

gmxoilav2#

我偶然发现了同样的问题,我在eclipse中使用了最新的Spring Boot - Java 17、Gradle 7.8、lombok 1.18.26和mapstruct 1.5.3.Final
以下是我尝试和工作的结果
1.在eclipse - gradle中设置注解处理器路径。
右键单击项目,然后单击“运行方式”-〉“运行配置”-〉“Gradle”-〉“在下面添加任务”

eclipseJdtApt 
eclipseFactorypath 
eclipseJdt

1.刷新Gradle项目
1.启用注解处理,如下x1c 0d1x
4.对于gradle,添加以下依赖项

implementation "org.mapstruct:mapstruct:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}"
   
annotationProcessor "org.projectlombok:lombok:${lombokVersion}", "org.projectlombok:lombok-mapstruct-binding:${lombokMapstructBindingVersion}", "org.mapstruct:mapstruct-processor:${mapstructVersion}"

1.刷新Gradle项目。
1.确保在Java编译器-〉AnnotationProcessing -〉FactoryPath中首先是lombok依赖项,然后是mapstruct
希望有帮助!:)

相关问题