添加新包破坏了我的 Spring Boot 应用程序

问题描述 投票:0回答:1

自从我在包含 Mapstructs 映射器的 Spring Boot 应用程序中添加了一个新包以来,我遇到了麻烦。看起来像田野:

@Autowired    
private UtenteMapper utenteMapper; 

未填充到我的服务组件中。 Spring boot 主类

PaniECumpanagghiuApplicationConfiguration
非常重要(它只包含
@SpringBootApplicationAnnotation
注释),但我之前尝试过添加
@ComponentScan

@SpringBootApplication    
@ComponentScan("PaniECumpanagghiu.PaniECumpanagghiu.Mappers")

通过这种方式,功能正在恢复,但

@Controller
并未被触发(当我调用 API 时,服务器检索到 404)。我还尝试将所有包添加到注释中
@ComponentScan
,就像这样

@ComponentScan("PaniECumpanagghiu.PaniECumpanagghiu.Mappers","PaniECumpanagghiu.PaniECumpanagghiu.controller",...)

通过这种方式,前一个对象

UtenteMapper
都不再被填充(与没有
@ComponentScan
注释的情况相同的行为)。

如果我注释对新包的所有引用,并简化 API 以仅返回字符串(“Ciao”),则控制器将被触发。这是我的树目录

Work tree

我已经在上一节中描述了所有内容。

编辑1 嘿伙计们,我的印象是问题出在映射结构依赖项导入中。 所以我决定给你整个 pom。

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://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.3.2-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>PaniECumpanagghiu</groupId>
    <artifactId>PaniECumpanagghiu</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>PaniECumpanagghiu</name>
    <description>Web app di food delivery per la sicilia sud orientale</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>22</java.version>
        <org.mapstruct.version>1.6.2</org.mapstruct.version>
    </properties>
    <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-security</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</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-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>
    </dependencies>

    <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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source> <!-- depending on your project -->
                    <target>1.8</target> <!-- depending on your project -->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <!-- other annotation processors -->
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

</project>
java spring spring-boot mapstruct
1个回答
0
投票

因为你说问题可能与 Lombok 和 Mapstruct 的交互有关,也许(

pom.xml
文件说
other annotation processors
,我不知道是否真的还有其他),你必须包括两个注释处理器在
maven-compiler-plugin
中,像这样:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <annotationProcessorPaths>
        <path>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>${lombok.version}</version>
        </path>
        <path>
          <groupId>org.mapstruct</groupId>
          <artifactId>mapstruct-processor</artifactId>
          <version>${org.mapstruct.version}</version>
        </path>
      </annotationProcessorPaths>
    </configuration>
  </plugin>

此外:

UtenteMapper
componentModel = "spring"
注释,如下所示:

@Mapper(componentModel = "spring")
public interface UtenteMapper {

如果没有该属性集(请阅读 javadoc),生成的映射器将无法作为 Spring bean 进行管理,并且不会被注入

@Autowired

顺便说一句,只是为了约定:请使用小写开头的包,我想 Eclipse 在创建类和其他包时已经将此作为警告。

© www.soinside.com 2019 - 2024. All rights reserved.