简体   繁体   English

Spring 引导无法识别添加的依赖项

[英]Spring boot won't recognize an added dependency

I am trying to add the ModelMapper dependency via pom.xml , however, when I try create a new instance of modelMapper it doesn't recognize the dependency and instead tries to import modelMapper from Swagger .我正在尝试通过pom.xml添加ModelMapper依赖项,但是,当我尝试创建 modelMapper 的新实例时,它无法识别依赖项,而是尝试从Swagger I have tried adding it manually and still I get the same problem.我已经尝试手动添加它,但我仍然遇到同样的问题。

XML 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                -->

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.12</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com</groupId>
    <artifactId>golden.scent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>something</name>
    <description>Demo project for Spring Boot</description>

    <!--                 PROPERTIES                -->

    <properties>
        <java.version>11</java.version>
    </properties>

    <!--                 DEPENDENCIES                 -->

    <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-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <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>
        <!-- https://mvnrepository.com/artifact/org.modelmapper/modelmapper -->
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.8</version>
        </dependency>


    </dependencies>

    <!--                 BUILD                 -->

    <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>

</project>

Configuration配置

import springfox.documentation.swagger2.mappers.ModelMapper;


@Configuration
public class Config {

    @Bean
    public ModelMapper modelMapper(){
        return new ModelMapper()


    }
}

Tried using the invalidate cache/restart which usually solves the problem.尝试使用通常可以解决问题的invalidate cache/restart not this time.这次不行。

I am trying to use DTO beans and do not want to write a mapper manually.我正在尝试使用 DTO bean,并且不想手动编写映射器。

I have tried looking for solutions but could not find one yet.我已经尝试寻找解决方案,但还没有找到。

Thanks谢谢

Change your class to the following:将您的 class 更改为以下内容:

import org.modelmapper.ModelMapper;

@Configuration
public class Config {
    @Bean
    public ModelMapper modelMapper(){
        return new ModelMapper();
    }
}

If with this you can't compile your code, you might need to delete org/modelmapper folder in the .m2 folder in your machine and reload your maven dependencies again.如果这样您无法编译您的代码,您可能需要删除机器中.m2文件夹中的org/modelmapper文件夹,然后再次重新加载 maven 依赖项。

You will have to go to your.m2 folder on your machine depending if you are using windows it should be in C:/Users/username/.m2 and delete the content of that folder.如果您使用的是 windows,则必须将 go 放到您机器上的 your.m2 文件夹中,它应该位于 C:/Users/username/ 中并删除该文件夹的内容。

Of course, you need to be sure that you have proper dependencies of Swagger in your pom.xml that are complaining about, if you do have all dependencies, you can go to the next step.当然,您需要确保在您抱怨的 pom.xml 中有正确的 Swagger 依赖关系,如果您确实有所有依赖关系,则可以 Z34D1F91FB2E514B8576FAB1A75A89A 到下一步。

Go back to your IntelliJ and you can run two maven commands ( clean, install ), you can either run them from the terminal of your IntelliJ, or you can run them from maven UI which is located on the top right side of the IntelliJ, next to database UI. Go 返回您的 IntelliJ,您可以运行两个 maven 命令(清理、安装),您可以从 IntelliJ 的终端运行它们,也可以从位于 IntelliJ 顶部的 Z402C5D9AF6B43717EA070BEE51 的 Z402C5D9AF6B43717EA070BEE51在数据库 UI 旁边。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM