javaspring-bootspring-mvchttp-status-code-404servletexception

spring boot application running but endpoint throwing 404


I was trying my hand at writing a sample Spring Boot application.

My initial thought was it's an easy example to follow and write but I am not able to print a simple statement upon hitting my endpoint.

I don't want to use any view (HTML,JSP etc) right now as I want to learn the backend first.

I have followed:

However, still stuck after a whole day of googling.

So let me reiterate the steps, I did.

  1. Initialized a spring boot project from spring.io and downloaded zip.
  2. Extracted the zip and imported it into Eclipse.
  3. Configured a Rest Controller with an endpoint to just print a hello world statement.
  4. Tomcat 9.0.56 is present on my system.
  5. Running TodoApplication as Java application or Spring boot.
  6. URL I am hitting is http://localhost:8080/greet
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {
    @GetMapping("/greet")
    public String sayHelloWorld() {
        return "Hello World";
    }
}

Spring Boot Application:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TodoApplication {

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

}

pom file:

<?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>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.sample</groupId>
    <artifactId>todo</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>todo</name>
    <description>Back-end for To DO App</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>
    </dependencies>

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

After navigating greet URL:

URL not present error

Got the following exception at console:

Spring boot Circular view error


Solution

  • So the solution as pointed out by xerx593 is below:

    1. Make sure to select packaging as war/jar in https://start.spring.io/.
    2. Import the zip file.
    3. Add your controller class in same or child package of SpringBootApplication, refer this for more info https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.structuring-your-code.
    4. Open terminal and change directory to working folder.
    5. Run mvn spring-boot:run if using maven.
    6. Hit the endpoint for example http://localhost:8080/greet or curl request.

    Also I have noticed that 8080 port is listening but spring boot fails to register on it, in this case set

    server.port = port other than 8080 
    

    in application.properties.

    8080 listening

    application properties file