【问题标题】:Why cant i import @RestController spring boot为什么我不能导入@RestController spring boot
【发布时间】:2020-05-19 22:31:37
【问题描述】:

enter image description here我只是想建立一个简单的带有休息控制器的弹簧启动应用程序。但无法导入 Rest Controller。这是我的主要方法

package com.test.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);
    }

}

这是我的 build.gradle。该脚本收集类路径上的所有 jar 并构建一个 jar。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.1.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'gs-spring-boot-docker'
    version =  '0.1.0'
}

repositories {
    mavenCentral()

}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.springframework.boot:spring-boot-starter-web'

    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

有什么想法吗?谢谢

【问题讨论】:

  • 什么错误?编译错误?顺便说一句,您通常不会在 SpringBoot 应用程序主类中使用“RestController”。定义一个新类(即 MyRestController),它将定义您的端点
  • 如果我在端点中添加错误是“错误:找不到符号@RequestMapping("/")"

标签: spring-boot spring-restcontroller


【解决方案1】:

您缺少 @RequestMapping("/") 的导入。

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

【讨论】:

  • 是的,不,仍然在导入的 Web 部分上出现红色突出显示。我在描述中添加了屏幕截图。我没有缺少依赖项吗?
  • 这些是依赖org.springframework.boot:spring-boot-starter-web的一部分。确保您的项目已编译并具有这些依赖。
  • 对不起,是的。我无效并重新启动了有效的缓存!感谢您的帮助!
【解决方案2】:

我无效并重新启动了有效的缓存!

【讨论】:

    猜你喜欢
    • 2021-01-10
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2018-08-30
    • 2020-12-23
    • 2018-01-12
    • 2021-11-24
    • 2020-04-24
    相关资源
    最近更新 更多