Spring Boot annotation @GetMapping can not be reso

2019-08-26 10:35发布

I tried creating a Spring Project from Spring Initializr for the first time in my local computer. But I am getting these errors @GetMapping cannot be resolved to a type.

My pom.xml file-

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>



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

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

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

    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>

</dependency>


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

Can someone help me with this? Is there some dependency I am missing?

2条回答
爷、活的狠高调
2楼-- · 2019-08-26 10:42

You have all dependenices which required for spring boot to run and I used your same pom and its worked, few points:

  1. If you have

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

then not required:

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

    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>

</dependency>

you can delete these dependencies as starter includes it.

  1. Also add maven plugin

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

and after this from command line: run mvn clean install 3. then try to rebuild project 4. if still do not work then move your starter-web as first dependency and again check.

查看更多
Summer. ? 凉城
3楼-- · 2019-08-26 10:46

I recommend you to start with https://start.spring.io/ for Spring Initalizr project. You're missing this dependency

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
查看更多
登录 后发表回答