Spring Boot Error 404 Not Found GetMapping

Viewed 30

I am new to spring boot and am trying to use the RestController to create API Endpoints. When I try to call the endpoint, it gives me error 404 not found which results to While label error page being displayed in the browser

Application Class:

package com.guider.guiderfinder;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("com.guider")
public class GuiderFinderApplication {

    public static void main(String[] args) {
        SpringApplication.run(GuiderFinderApplication.class, args);
    }

}

Controller Class:

package com.guider.guiderfinder.controller;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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;

import com.guider.guiderfinder.model.Guide;
import com.guider.guiderfinder.repository.GuideRepository;

@RestController
@RequestMapping("/api/v1/") //Standard url endpoint defined
public class GuideController {
    
    @Autowired
    private GuideRepository guideRepository;
    
    @GetMapping("/guides") //this api endpoint is called from url = localhost:8080/api/v1/guides
    public List<Guide> getAllGuides() {
        System.out.println("API was hit");
        return guideRepository.findAll();
    }

}

pox.xml:

4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.3 com.guider guider-finder 0.0.1-SNAPSHOT guider-finder Backend of guide finder <java.version>17</java.version> org.springframework.boot spring-boot-starter

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        </dependency>
    
    <!-- https://mvnrepository.com/artifact/net.sourceforge.javydreamercsw/MySQL-Driver -->
    <dependency>
        <groupId>net.sourceforge.javydreamercsw</groupId>
        <artifactId>MySQL-Driver</artifactId>
        <version>0.1</version>
    </dependency>
</dependencies>

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

When you write @ComponentScan("com.guider") you are telling Spring to only scan in this directory. Your controller is in a sub-directory. Using @ComponentScan(basePackages = "com.guider") should solve your problem. An alternative is to add additional packages to scan. An example of this that would pick up your controller is @ComponentScan({"com.guider", "com.guider.guiderfinder"}).

Related