Why do I always get Whitelabel Error Page with status "404" while running a simple Spring Boot Application
Asked Answered
M

7

6

My Controller

@Controller
//@RequestMapping("/")
//@ComponentScan("com.spring")
//@EnableAutoConfiguration
public class HomeController {

    @Value("${framework.welcomeMessage}")
    private String message;

    @RequestMapping("/hello")
    String home(ModelMap model) {
        System.out.println("hittin the controller...");
        model.addAttribute("welcomeMessage", "vsdfgfgd");
        return "Hello World!";
    }

    @RequestMapping(value = "/indexPage", method = RequestMethod.GET)
    String index(ModelMap model) {
        System.out.println("hittin the index controller...");
        model.addAttribute("welcomeMessage", message);
        return "welcome";
    }

    @RequestMapping(value = "/indexPageWithModel", method = RequestMethod.GET)
    ModelAndView indexModel(ModelMap model) {
        System.out.println("hittin the indexPageWithModel controller...");
        model.addAttribute("welcomeMessage", message);
        return new ModelAndView("welcome", model);
    }
}

My JSP (welcome.jsp) inside /WEB-INF/jsp (parent folder is WebContent)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome to Spring Boot</title>
</head>

<body>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Message: ${message}
</body>
</html>

My pom.xml

<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>
    <groupId>SpringBootPlay</groupId>
    <artifactId>SpringBootPlay</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>
    <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>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.jcabi</groupId>
            <artifactId>jcabi-log</artifactId>
            <version>0.17</version>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
        <start-class>com.spring.play.BootLoader</start-class>
        <main.basedir>${basedir}/../..</main.basedir>
        <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

My App Initializer

@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan({ "com.spring.controller" })
@PropertySources(value = { @PropertySource("classpath:/application.properties") })
public class BootLoader extends SpringBootServletInitializer {

    final static Logger logger = Logger.getLogger(BootLoader.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(BootLoader.class);
    }

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

I even added thymeleaf dependency to my pom. It still didn't work. When ever I hit localhost:8080/hello or /indexPage or /indexPageWithModel it always says

Whitelabel Error Page

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

Wed Sep 21 21:34:18 EDT 2016 There was an unexpected error (type=Not Found, status=404). ]/WEB-INF/jsp/welcome.jsp

My application.properties

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
framework.welcomeMessage=Welcome to Dashboard

Please help me. Thanks!

Melly answered 22/9, 2016 at 1:37 Comment(1)
Maybe it is your package's naming problem. Check this answer https://mcmap.net/q/497918/-spring-boot-whitelabel-error-page-type-not-found-status-404Bullough
M
5

Figured it out myself.

When I converted my dynamic webproject into maven project it did not create the folder structure this way

src/main/java

and

src/main/resources

and

src/main/webapp

I manually created myself and moved the jsp files from WebContent/WEB-INF/jsp to src/main/webapp/WEB-INF/jsp and modified the Java build path in the project properties.

Then I restarted the embedded tomcat and tried it again. It worked.

Melly answered 22/9, 2016 at 2:52 Comment(0)
B
8

This is one of most common error that almost all the spring boot beginners face.

Solution to this is very simple, - your Bootstrap class should know the package or the class path where it should refer in order access the component/controller. Hence you need to specify like :- @ComponentScan(basePackages= {"org.test.controller"})

P.S.- here "org.test.controller" is a qualified name of the package where I have kept my controller.

Brahmana answered 11/8, 2018 at 19:20 Comment(1)
Nice! In my case it was in a sibling package from the main class.<br> I solved moving the controllers to the same package. Child package also works.Olivarez
M
5

Figured it out myself.

When I converted my dynamic webproject into maven project it did not create the folder structure this way

src/main/java

and

src/main/resources

and

src/main/webapp

I manually created myself and moved the jsp files from WebContent/WEB-INF/jsp to src/main/webapp/WEB-INF/jsp and modified the Java build path in the project properties.

Then I restarted the embedded tomcat and tried it again. It worked.

Melly answered 22/9, 2016 at 2:52 Comment(0)
E
3

General troubleshooting guide for the Whitelabel error page:

Enable trace logging for the Spring web handling. Add to your application.properties: logging.level.org.springframework.web.*=TRACE

With this you can see during startup if your controller is registered properly. For example having HelloController with one mapped GET method you should see on logs:

2020-08-20 08:56:55.731 TRACE 19687 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping :
    c.n.g.c.HelloController:
    {GET /hello}: sayHello()

If you see your controller methods logged, it is registered properly. If not, follow the other advices given for correct project structure.

If controller is properly registered but you still get the white label error page, it likely means one of these:

  • You are calling your endpoint with bad url/method/content type
  • There is an error happening on your endpoint
  • You are not returning proper response (which could cause Spring to show error page)

Which of these cases it is, should be revealed from the produced logs also. Happy hunting!

Eolic answered 20/8, 2020 at 6:12 Comment(0)
Y
2

Sometimes it might happen because of @Controller or @RestController annotation is missing.

Sample Program With @RestController Annotation

Yielding answered 22/12, 2021 at 6:13 Comment(2)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewGazehound
Yeah thanks, Dias I'll update images instead of linksYielding
A
1

Cause of this type of errors is due to Bootstrap class is not aware of the location of the controller where it needs to be looked.

In such cases, we need to specify the package or classpath which we can refer to using @ComponentScan(basePackages={"com.sample.controller"}) where In my case I have specified package as com.sample.controller.

Albata answered 14/6, 2019 at 9:12 Comment(0)
A
0

I added @Configuration annotation in my SecurityConfiguration class and it worked for me.

Apparel answered 7/6, 2022 at 12:46 Comment(0)
B
0

I got White Label for a completely different mistake.

Short Answer: i was always hitting "localhost:8080/Courses" instead of "localhost:8080/courses". ( note the Capital 'C' in courses)

so, i was doing some course on spring boot and when i added just "/course" and return a bunch of courses, it was working fine, after sometime i added some more methods to use the path variable and suddenly the /courses stopped working.

it was because, i used "localhost:8080/Courses" ( with a capital C) once and it got saved in the browser recommendation. whenever i used "localhost:8080/courses" it was auto-corrected to "/Courses" and didn't notice it for a very long time.

Brahear answered 10/7, 2024 at 18:48 Comment(1)
Sharing this so that no one should waste time on this simple mistake.Brahear

© 2022 - 2025 — McMap. All rights reserved.