Home > Software engineering >  Spring Boot Controller not mapping (Whitelabel Error Page)
Spring Boot Controller not mapping (Whitelabel Error Page)

Time:11-28

I have a Spring REST project that is redirecting all requests to error page, even if they are mapped in the controller.

I reduced the code to the smallest possible version that produces the error:

Here is the project structure:

enter image description here

Here is the Application class (The imports are removed to make the thread easier to read):

package com.example.demo;

@Controller
@SpringBootApplication
public class TestApplication {


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


    @GetMapping("/greeting")
    @ResponseBody
    public String greeting() {
        return "greeting";
    }

}

Originally I hade a sperate controller from the App class, but moved the controller code to the app class to make sure that this is not a project structure problem Here is the controller code (Tried with and without it, and received the same error):

@Controller
@SpringBootApplication
public class TestApplication {


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


    @GetMapping("/hello")
    @ResponseBody
    public String greeting() {
        return "greeting";
    }

}

(Both http://localhost:8080/greeting as well as well http://localhost:8080/hello return the same error page)

Dependencies and plugins from the pom file:

        <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>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
    

(Tried with and without tomcat as dependency and nothing changed)

And lastly here is the error message I receive in the browser when I visit the links (http://localhost:8080/greeting and http://localhost:8080/hello):

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Nov 27 00:16:08 CET 2022
There was an unexpected error (type=Not Found, status=404).

CodePudding user response:

Did you tried making call to the endpoint via postman ? If so, can you try again after removing @ResponseBody annotation.

CodePudding user response:

Instead of @Controller use @RestController

  • Related