Issue
I started learning Spring Framework and get this kind of error while running localhost:8080
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
Controller.java
package com.springFramework.helloGame.controller;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
@GetMapping("/sum")
public long displaySum(){
return 100;
}
}
HelloGameApplication.java
package com.springFramework.helloGame;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class HelloGameApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(HelloGameApplication.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 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.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springFramework</groupId>
<artifactId>helloGame</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloGame</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
my project structure looks fine, but don't know why I got error! Please help me!
Solution
The Whitelabel page is a spring error handling method and it tries to hint to you that you do have a mapping for /error. Now coming to the issue you need to write @RequestMapping("/") above your get mapping in the controller so that the spring knows the base URI. It's an entry point for the spring application for the browser.
Now you have written /sum get URI and in the browser, you are trying for http://localhost:8080 So for the above Uri to work you need to provide @RequestMapping("/")
or you can use http://localhost:8080/sum with you current code.
Answered By - Ankit Dhar