【问题标题】:Spring Boot Application cannot be resolved to a type but I imported it in pom.xmSpring Boot Application 无法解析为类型,但我将其导入 pom.xm
【发布时间】:2017-02-17 12:40:33
【问题描述】:

我在 pom.xml 中添加了 spring boot,但它仍然报错。 谁能帮帮我。

报错 The import org.springframework.boot.autoconfigure.SpringBootApplication cannot be resolved

和 SpringBootApplication 无法解析为类型

我的绒球。 xml是

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.webapp</groupId>
  <artifactId>proj1</artifactId>
  <packaging>war</packaging>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>
  <version>0.0.1-SNAPSHOT</version>
  <name>proj1 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
  </dependencies>
  <build>
    <finalName>proj1</finalName>
  </build>
</project>

【问题讨论】:

  • 您缺少“spring-boot-autoconfigure”jar,不知道为什么。您可以检查它是否在 .m2 目录中。如果是,我会删除它并重新导入项目。
  • 我试过了,没用。我在 eclipse 中尝试了相同的代码,没有错误,但是 STS 给出了错误。
  • @Archit,将我的答案 (stackoverflow.com/questions/39245732/…) 链接到这个问题。
  • 从命令行运行“mvn clean package”会编译吗?

标签: maven spring-boot pom.xml


【解决方案1】:

你的 pom 看起来几乎是正确的,但需要注意的地方很少

  1. 不需要添加依赖'javax.servlet-api',它可以从'spring-boot-starter-web'获得
  2. 您可能缺少“spring-boot-maven-plugin”,它不是强制性的,但您可能希望在您的项目中使用它。它提供了一些简单的 Maven 目标供您运行。

       <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

如果你正在运行 eclipse,你可以像往常一样生成你的 .classpath 文件 mvn eclipse:eclipse,一旦完成,你的 ide 应该看起来不错

然后 mvn spring-boot:run 应该设置您的应用程序运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2020-09-13
    相关资源
    最近更新 更多