0

I am a Java newb and a Spring Boot newb. Lots of learning going on as I try to learn a new language. I know Pytnon and JS as reference points.

I am in the midst of trying to copy over code from an abandoned CRUD project into a new one. I am working through the start of a long and winding series of bugs, and the first on the chopping block is that import org.springframework.web.bind.annotation.GetMapping; highlights red over web and says Cannot resolve symbol 'web' when I mouse over it.

Here is some code with comments indicating where the red is

package com.example.bookscrud.book;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; // here
import org.springframework.web.bind.annotation.PostMapping; // here 
import org.springframework.web.bind.annotation.RequestBody; // here
import org.springframework.web.bind.annotation.RestController; // here

import java.util.List;


@RestController
@RequestMapping(path = "api/v1/books")
public class BooksController {
    private final BookService BookService;

    @Autowired // magic
    public BooksController(BookService BookService) {
        this.BookService = BookService;
    }


    @GetMapping(value = "/")
    public List<Book> getBooks() {
        return BookService.getBooks();
        // return List.of(Book, bart);
    }

}

I made some attempts to modify the pom.xml thinking maybe I missed some crucial import. But no, doesn't look that way.

<?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 https://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>3.1.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>books-crud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>books-crud</name>
    <description>Books CRUD app</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

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

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

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

</project>

I have a similar problem in another file: import org.springframework.jdbc.datasource.DriverManagerDataSource; gives "Cannot resolve symbol 'jdbc'" on mouseover, and the imports show red.

mvn package says something about an unrelated error, making me think its just an IntelliJ IDEA issue. Here's the error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method BookRepository in com.example.bookscrud.book.BookRepositoryImpl required a bean of type 'javax.sql.DataSource' that could not be found.


Action:

Consider defining a bean of type 'javax.sql.DataSource' in your configuration.

I am using IntelliJ IDEA as my IDE.

So, I tried modifying the pom.xml, also tried mvn clean install -U, and restarting my IDE. I would try more things but, being a newb at Java, I'm not sure what to do!

Additionally, googling this doesn't return much. I'm still looking though, have been for a few hrs.

edit: issue while importing org.springframework.web : cannot resolve symbol 'web' looks related, but it's 4 years old and unanswered!

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
plutownium
  • 1,220
  • 2
  • 9
  • 23
  • Try clicking on the "reload project" in the maven tab. If that doesn't help, delete your `.m2` maven cache and try again. – Kayaman Aug 26 '23 at 06:35
  • It doesn't cause your problem, but if you're using Spring Boot you should not be declaring versions for dependencies managed by Spring Boot, like that `spring-web` dependency. Remove its `version` element. – Mark Rotteveel Aug 26 '23 at 10:46

2 Answers2

0

I tried right clicking the red and choosing "Context Actions" from the IDEA dropdown and then picking something about looking on the web for the package. I don't recall what the specific text said. Anyway: It worked! My red error text is gone. Oddly git diff shows 0 changes so I guess it was an IDEA centric issue and not the code or pom.xml file

plutownium
  • 1,220
  • 2
  • 9
  • 23
0

You might have some problem with network, maven doesn't download dependencies correctly.

Check your local maven repository, is there any .lastUpdated file.

If there is, delete it and reload your maven project.

And this question might help.

Kevin Smith
  • 1
  • 1
  • 1