0

I'm getting 404 with this code.

Controller class

@RestController
public class TestAPI {

@RequestMapping(method = RequestMethod.GET, value = "/")
    public ResponseEntity<String> hello() {
        System.out.println("Hit me!");
        return new ResponseEntity<String>("Hello, you!", HttpStatus.OK);
   }
}

Application startpoint:

@SpringBootApplication
public class DemoApplication {

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

}

pom.xml

<?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.1.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <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-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

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

The dispatcherServlet is initializing while hitting 8080 on localhost but, it is not calling get method. The error is 404, not found. I have tried mapping it with @GetMapping and with @RequestMapping, still not able to find the solution.

  • 2
    What is the package name for `TestAPI` and `DemoApplication` classes? – Karol Dowbecki May 23 '19 at 11:03
  • Karol, has already given hints. – Sambit May 23 '19 at 11:07
  • @KarolDowbecki com.example.demo for DemoApplication and com.example.demo.api for TestAPI. – Hakunamatatatata May 23 '19 at 11:11
  • Add `@ComponentScan(basePackageClasses = TestAPI.class)` after the `@SpringBootApplication` annotation. A similar question is already answered [Spring Boot: Cannot access REST Controller on localhost (404)](https://stackoverflow.com/questions/31318107/spring-boot-cannot-access-rest-controller-on-localhost-404) – Sudhir Ojha May 23 '19 at 11:14
  • @KartikDutt can you confirm that the Java package is a proper sub-directory e.g. `com/example/demo` and not a single directory with dots in the name? – Karol Dowbecki May 23 '19 at 11:18
  • @SudhirOjha the DemoApplication.java is the base class. You need not to add `@ComponentScan` if the base class is annotated with `@SpringBootApplication`. Correct me if I'm wrong. – Hakunamatatatata May 23 '19 at 11:21
  • `ComponentScan` is not at all required, you controller does not have parent request mapping,instead it has direct controller mapping, hence you url is nothing but the root-content path of the application – silentsudo May 23 '19 at 12:00

0 Answers0