'Spring Boot Whitelabel Error page (type=Not Found, status=404)
Good afternoon! I'm starting spring studies, I'm following a tutorial the same way, but it returns an error:
Folder structure:
The strange thing is: If I insert the "EventoController.java" into br.com.SpringApp.SpringApp, it works correctly.
package br.com.SpringApp.SpringApp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringAppApplication {
public static void main(String[] args) {
SpringApplication.run(SpringAppApplication.class, args);
}
}
.
package br.com.SpringApp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class EventoController {
@RequestMapping("/cadastroEvento")
public String form() {
return "evento/formEvento";
}
}
As requested, I'm adding 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.SpringApp</groupId>
<artifactId>SpringApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringApp</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</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>
Can someone tell me where I'm wrong, please?
Solution 1:[1]
Make sure that your main class is in a root package above other classes.
When you run a Spring Boot Application, (i.e. a class annotated with @SpringBootApplication), Spring will only scan the classes below your main class package.
So your declaration goes like this
package br.com.SpringApp.SpringApp;
inside this main class i.e SpringAppApplication
package br.com.SpringApp.SpringApp.controller;
name of your controllers i.e EventoController & indexControllers
package br.com.SpringApp.SpringApp.model;
name of your models i.e Evento
After This clean your project and re-run spring boot application;
Solution 2:[2]
Solution:
If you are using @Controller over the Controller class then it will be treated as a MVC controller class. But if you want a special controller used in RESTFul web services then you to use @Controller along with @ResponseBody annotation or you can directly use @RestController over the Controller class. It worked for me as I was getting the same error while creating SpringBoot project with RestFul webservices.
package br.com.SpringApp.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class EventoController {
@RequestMapping("/cadastroEvento")
@ResponseBody
public String form() {
return "evento/formEvento";
}
}
or:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
public class EventoController {
@RequestMapping("/cadastroEvento")
public String form() {
return "evento/formEvento";
}
}
Solution 3:[3]
verify that you have the correct thymeleaf dependency within your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Solution 4:[4]
As answered by Deepak, main class should be in a root package above other packages. But if you don't want to do this, you can use:
@SpringBootApplication(scanBasePackages = {"com.other.packages","com.other"})
public class SpringAppApplication {
.....
}
Solution 5:[5]
As a beginner to the SpringBoot, this error can be happened to anyone at least one time, because for a few reasons.
- The Application class (eg: SpringAppApplication.java) should be at the root level, as well as the controller classes can be placed under the same level or maybe in a subdirectory. This application class should be denoted using
@SpringBootApplicationorextends SpringBootApplication - You may miss
@RestControllerannotation at top of the controller class. - Or can be missed the mapping annotation.(
@PostMapping("/save")or@GetMapping("/id")etc.) - finally just check whether you are entering the correct request mapping address(URL) you may miss a slash or
@RequestMapping("/customer")(if the annotation available in your controller class) or spellings error in the URL.
Solution 6:[6]
I mistakenly give name parameter in my GetMapping and PostMapping
@GetMapping(name: "/all")//wrong code
Actually, you need to specify the path directly
@GetMapping("/all") //correct code - remove name
Hope this mistake may help anyone in future!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dipak Thoke |
| Solution 2 | Claude |
| Solution 3 | senape |
| Solution 4 | JasonD |
| Solution 5 | Ruchira Supipi |
| Solution 6 | Ranjithkumar |


