Search code examples
javaspring-bootspring-mvcgetspring-restcontroller

How to fix "Whitelabel Error Page" on spring framework - GET request is not working


I'm trying to make a "Hello World" controller with spring boot and the GET request is not working. What can i do to fix this?

I used spring initializer at https://start.spring.io/ and in the Dependencies I chose web

I've tried to use diffrent annotations like @GetMapping

package com.example.hello;

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

@RestController
public class HelloController {

    @RequestMapping("/")
    public String helloWorld() {
        return "Hello World";
    }
}

<?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.3.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>
    </dependencies>

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

</project>

package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

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

}

I want the output to be "Hello World" but the actual output is "Whitelabel Error Page"


Solution

  • The issue is related to your project structure, by default Spring Boot will scan the components below your main application class.

    In your case your main is located at package com.example.demo;, and your controller is located at package com.example.hello;.

    I would recommend you to keep your controller in a new package below the com.example.demo package, like com.example.demo.controller as per structure mentioned in the documentation.

    If you really want to use the com.example.hello package, you may need to add the @ComponentScan("com.example.hello"), which becomes hard to mantain in the future.

    More info can be found here.