21

So I have this code

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

And I already have the following in my POM file

<packaging>war</packaging>
    <properties>
        <spring.version>4.3.0.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>

Yet when I built it ends up complaining package org.springframework.web.bind.annotation does not exist

Why? I already added spring web as a dependency. What am I doing wrong?

MassiveParty24
  • 211
  • 1
  • 2
  • 3

6 Answers6

41

I had a similar issue, I fixed it by going into my pom.xml file and changing the artifactID for the spring boot dependency from:

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

to

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

Edit to show explanation:

I was following a tutorial that had me using the wrong dependency. If I remember correctly, the spring-boot-starter-web dependency contains everything spring-boot-starter has, plus what you need to make a basic web app. So my app was looking for some web dependencies that I'm assuming spring-boot-starter does not contain.

Cory Bergquist
  • 445
  • 4
  • 12
  • 3
    Why does this work? And how did you find this fix? I find this to be very obscure and strange. Especially because I just downloaded the starter project Spring provides... – fritzvd Sep 06 '21 at 10:01
  • 3
    In my particular case, I was following a tutorial that had me using the wrong dependency. If I remember correctly, the spring-boot-starter-web dependency contains everything spring-boot-starter has, plus what you need to make a basic web app. So my app was looking for some web dependencies that I'm assuming spring-boot-starter does not contain. – Cory Bergquist Sep 07 '21 at 14:52
  • 2
    Indeed! I finally stumbled upon this and -- thanks for that, Cory. I was following the tutorial from spring.io and -- typical of Java tutorials, it is out of date and has not been updated to reflect the apparent new packaging. Maven and spring are supposed to make things easier, but -- I have yet to see that. – jinzai Feb 04 '22 at 17:47
  • I used the generated code from spring official website and got this error... – Siwei May 01 '23 at 01:14
8

Run the following command

mvn clean install

If you are using IDE like intelliJ idea or Eclipse make sure to re import the project. Here is an answer how to refresh maven dependencies in eclipse

How to update maven repository in Eclipse?

mirmdasif
  • 6,014
  • 2
  • 22
  • 28
1

For me, removing the spring-web dependency worked. To do this, remove the following from your pom.xml:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
</dependency>
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
Rabina
  • 11
  • 1
1

Clean install doesn't fixed issue for me. I clicked in Maven (right tab in Intelij) and then refresh button. Maven downloaded everything and it works now

  • Clean the refresh button to reload maven project works for me. Directly click the `pom.xml` and select `reload from disk` seems not to reload the mvn project but to reload the single file. – chunyang.wen Oct 24 '22 at 06:12
0

Trying:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.6.0</version>
        </dependency>
<?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>

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

    <groupId>com.pxample</groupId>
    <artifactId>pemo</artifactId>
    <version>0.1.36-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>

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

    </dependencies>

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

And view the samples below:

https://github.com/spring-guides/gs-spring-boot/blob/main/initial/pom.xml

https://github.com/spring-guides/gs-multi-module/blob/main/complete/pom.xml

Nick Dong
  • 3,638
  • 8
  • 47
  • 84
0

i changed

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>

to

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>
Parham
  • 1
  • 1