Creating basic example from spring.start.io returns 404 instead of mapped String

.

I’m unable to route requests to my Spring Boot application, even though I’ve tried the following:

The pom.xml and RestController class are provided below for reference.

  • My 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <packaging>jar</packaging>
   
   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.2.2.RELEASE</version>
       <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>myrest</groupId>
   <artifactId>remoteapi</artifactId>
   <version>0.0.1</version>
   <name>remoteapi</name>
   <description>Rest Mobile Dev ICSD Api</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>
           <exclusions>
               <exclusion>
                   <groupId>org.junit.vintage</groupId>
                   <artifactId>junit-vintage-engine</artifactId>
               </exclusion>
           </exclusions>
       </dependency>
   </dependencies>

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

</project>

  • My RestController class

package Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MainController {

    
    
    
    @RequestMapping(value="/api")
    public String test()
    {
        return "Hello World";
    }
}


I’m unable to route requests to my Spring Boot application. I’m accessing it from localhost:8080/api and the MainController class is located within the Controller package.

I have tried the following solutions suggested on Stack Overflow, but none of them worked:

I have not encountered this issue before, as I have successfully run the same method with the same mapping on an older project.

The issue might be related to the package structure. The RestController class is located within the Controller package, but the package name is not defined in the @RequestMapping annotation. Try updating the annotation to @RequestMapping(value="/api", package="Controller").