【问题标题】:Cannot resolve @GetMapping in Spring tool Suite无法解析 Spring 工具套件中的 @GetMapping
【发布时间】:2019-11-04 20:14:24
【问题描述】:

我在 Spring tool Suite 中创建了一个 spring 项目。

即使添加了它显示的所有依赖项也无法解析@GetMapping

错误信息

GetMapping cannot be resolved to a type

我的代码是

package tacos;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController 
{
    @GetMapping
    public String home()
    {
        return "home";
    }
}

正如您在这一行中看到的,我已经导入了 GetMapping。

import org.springframework.web.bind.annotation.GetMapping;

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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>sia</groupId>
    <artifactId>taco-cloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>taco-cloud</name>
    <description>Taco cloud example</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

我已经尝试过这篇文章中提供的解决方案

Spring Boot annotation @GetMapping can not be resolved to a type

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    因为您从未将 Spring MVC 包含到您的项目中,因此它无法解析来自它的类。

    您应该在pom.xml 中包含spring-boot-starter-web,这将为您传递获取Spring MVC 和嵌入式Tomcat:

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    

    【讨论】:

    • 成功了。但是现在在说 The import org.springframework.web.bind.annotation.GetMapping is never used 下面有一条黄线。 @KenChan
    • 黄线只是警告,你可以忽略它
    • @KenChan 您已经导入了两次 GetMapping。按Ctrl+Shift+O 自动修复导入。
    • @helospark 。感谢您的提示????
    猜你喜欢
    • 1970-01-01
    • 2018-05-09
    • 2013-02-22
    • 2022-12-25
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 2018-10-13
    相关资源
    最近更新 更多