spring import org.springframework.web.bind.annotation.RestController 无法解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29075574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:27:01  来源:igfitidea点击:

import org.springframework.web.bind.annotation.RestController cannot be resolved

springspring-mvc

提问by sudha

I'm using Eclipse Kepler..I tried to use import org.springframework.web.bind.annotation.RestController But this import statement is not getting resolved I've added all the dependencies required for that..but restcontroller is not getting resolved..

我正在使用 Eclipse Kepler ..我尝试使用 import 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;
  }
  }

And my pom.xml is

我的 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">

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>

Thanks in advance..

提前致谢..

回答by nijave

The problem is your class is named "RestController" and that conflicts with the actual RestController class used for the annotation. You just need to rename your class/file to something else like "DemoController" to avoid the class name conflict or specify the full path like @org.springframework.web.bind.annotation.RestController so it knows which one to use

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

回答by Ahmed AMMOURI

I had the same issue i found the solution here. Add this dependency to the pom.xml

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

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

回答by david

@RestController annotation since spring-web 4.0

@RestController 注解自 spring-web 4.0

回答by vaibhavi belur

for a simple boot application,just check these dependencies in your pom.xml:

对于简单的启动应用程序,只需在 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>

回答by Niran

Sometimes .jar files does not get downloaded via Maven properly. Please perform maven update on the project.

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

回答by Vikash

I was having similar problem using springboot, my spring version in Pom was

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

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

I changed the version to <version>1.5.9.RELEASE</version>

我把版本改成 <version>1.5.9.RELEASE</version>

clean the project after that and import, that would work.

之后清理项目并导入,这会起作用。

回答by hw.ralf79

Why don't you change spring mvc version?

你为什么不改变 spring mvc 版本?

I had experienced the same problem using springboot. In my case the problem is version. So I had change version 1.2.3 -> 1.2.2. Then the problem was solved.

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

回答by MR AND

update properties in pom.xml as

将 pom.xml 中的属性更新为

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