package org.springframework.web.bind.annotation does not exist even though it's defined in POM
Asked Answered
S

6

24

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?

Singlefoot answered 7/9, 2017 at 4:30 Comment(1)
Have you updated maven ? try mvn installLeishmaniasis
I
47

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.

Insignificance answered 7/1, 2021 at 21:51 Comment(4)
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...Apotheosize
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.Insignificance
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.Coitus
I used the generated code from spring official website and got this error...Dexamyl
D
9

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?

Deena answered 7/9, 2017 at 4:49 Comment(0)
S
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>
Sayre answered 30/5, 2018 at 13:30 Comment(0)
C
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

Collado answered 13/9, 2022 at 11:3 Comment(1)
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.Scripture
I
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

Invective answered 23/11, 2022 at 5:5 Comment(0)
I
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>
Infirm answered 2/1, 2023 at 6:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.