【问题标题】:import org.springframework.web.bind.annotation.RestController cannot be resolvedimport org.springframework.web.bind.annotation.RestController 无法解析
【发布时间】:2025-12-24 15:55:06
【问题描述】:

我正在使用 Eclipse Kepler..我尝试使用 导入 org.springframework.web.bind.annotation.RestController 但是这个导入语句没有得到解决 我已经添加了所有需要的依赖项..但是restcontroller没有得到解决..

package s;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RestController {



@Autowired Bean bean;
@RequestMapping(value="/springAngularJS", method=RequestMethod.GET)

public @ResponseBody Bean getBean() {      
Bean bean=new Bean();
    bean.setUserName("savitha");

    bean.setAge("22");

    bean.setCompany("Infosys");

    bean.setDesignation("ApplicationDeveloper");

    return bean;
  }
  }

而我的 pom.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>AngularRest</groupId>
<artifactId>AngularRest</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>AngularRest</name>
<url>http://maven.apache.org</url>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<repositories>
    <repository>
        <id>objectdb</id>
        <name>ObjectDB Repository</name>
        <url>http://m2.objectdb.com</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.4</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>
    <dependency>
        <groupId>com.objectdb</groupId>
        <artifactId>objectdb</artifactId>
        <version>2.0.4</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.6.10</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.5</source>
                <target>1.5</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>6.1.10</version>
            <configuration>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <stopKey>foo</stopKey>
                <stopPort>9999</stopPort>
            </configuration>
            <executions>
                <execution>
                    <id>start-jetty</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <scanIntervalSeconds>0</scanIntervalSeconds>
                        <daemon>true</daemon>
                    </configuration>
                </execution>
                <execution>
                    <id>stop-jetty</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>AngularRest</finalName>
</build>

提前谢谢..

【问题讨论】:

  • 请分享您项目的 pom.xml 或 jar 列表,如果您有 pom,最好分享您的 pom 依赖图。
  • 我遇到了同样的问题。我已经有一个 spring-web 依赖项,在删除它并添加 spring-boot 依赖项时问题就解决了。

标签: spring spring-mvc


【解决方案1】:

问题是您的类被命名为“RestController”,并且与用于注释的实际 RestController 类冲突。您只需要将您的类/文件重命名为“DemoController”之类的其他名称,以避免类名冲突或指定完整路径(如 @org.springframework.web.bind.annotation.RestController 以便它知道使用哪一个)

【讨论】:

  • 为我工作!谢谢=]
  • 超级!!,谢谢。
【解决方案2】:

我遇到了同样的问题,我找到了解决方案 here。 将此依赖项添加到 pom.xml

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.6.RELEASE</version>
    </dependency>

【讨论】:

  • 谢谢,这是缺少的。
  • 非常感谢。
  • 谢谢你,艾哈迈德
【解决方案3】:

@RestController 注解自 spring-web 4.0 起

【讨论】:

    【解决方案4】:

    有时 .jar 文件无法通过 Maven 正确下载。请对项目执行 Maven 更新。

    【讨论】:

      【解决方案5】:

      对于一个简单的启动应用程序,只需检查 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>
      
          <groupId>com.example</groupId>
          <artifactId>bootbtb</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <packaging>jar</packaging>
      
          <name>bootbtb</name>
          <description>bus project for Spring Boot</description>
      
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>1.5.9.RELEASE</version>
              <relativePath/> <!-- lookup parent from repository -->
          </parent>
      
          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
              <java.version>1.8</java.version>
          </properties>
      
          <dependencies>
          <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>4.1.6.RELEASE</version>
      </dependency>
      
              <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>
               <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-webmvc</artifactId>
              <version>4.1.6.RELEASE</version>
          </dependency>
      
      </dependencies>
      
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
      
      
      </project>
      

      【讨论】:

        【解决方案6】:

        我在使用 springboot 时遇到了类似的问题,我在 Pom 中的 spring 版本是

        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        

        我将版本更改为 &lt;version&gt;1.5.9.RELEASE&lt;/version&gt;

        之后清理项目并导入,这样就可以了。

        【讨论】:

          【解决方案7】:

          为什么不改spring mvc版本呢?

          我在使用 springboot 时遇到了同样的问题。 就我而言,问题是版本。 所以我改变了版本 1.2.3 -> 1.2.2。 然后问题就解决了。

          【讨论】:

            【解决方案8】:

            将 pom.xml 中的属性更新为

            <properties>
                <spring.version>4.0.0.RELEASE</spring.version>
               </properties>
            

            【讨论】:

              最近更新 更多