【问题标题】:Unable to import org.springframework.web.bind.annotation.RestController; Can't use @Restcontroller无法导入 org.springframework.web.bind.annotation.RestController;无法使用@Restcontroller
【发布时间】:2021-04-17 11:40:37
【问题描述】:

这是我的代码`

package com.example.demo3;

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

`@RestController`

public class BooksC {
 //Code
}

这是我的 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 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>2.3.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo3</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo3</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

而且我已经尝试过使用其他 RELEASE 以及版本和依赖项 但问题仍然存在 我只是想让你展示演示类 BooksC

的问题

这里我使用了 spring 初始化器

使用 Eclipse 版本:2021-03 (4.19.0)

【问题讨论】:

    标签: java eclipse maven spring-restcontroller


    【解决方案1】:

    你需要添加 REST 依赖

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

    【讨论】:

      【解决方案2】:

      您需要将 Spring-web 依赖项添加到您的 pom 文件中

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

      那么它应该可以正常工作

      【讨论】:

      • 现在比xx工作正常
      猜你喜欢
      • 2015-05-18
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      相关资源
      最近更新 更多