I developed a simple Hello World Spring MVC Web Application using Spring Boot (Spring Initializr - start.spring.io). I chose the following options at Spring Initializr:
- Project: Maven
- Language: Java
- Spring Boot: 3.1.2
- Dependencies: Spring Web
Clicked on Generate to download the starter app, extracted it and added:
- helloWorld.html- this just diplays Hello World! (Please see below for the GitHub Repo URL)
- Controller Java Class with one action method tagged with @RequestMapping("/helloWorld") that returns "helloWorld.html" (Please see below for the GitHub Repo URL)
Issue: When I try to hit the action method from the browser using the following url I get "Whitelabel Error Page": http://localhost:8080/helloWorld
Can you please tell me what I am missing?
GitHub Repo: https://github.com/angunda/hello-world-spring-web-app
I tried issuing GET /localhost:8080/helloWorld in InteliJ's HttpClient and got 404 NOT FOUND error instead of the helloWorld.html file.
I also issued the request from chrome using http://localhost:8080/helloWorld, however, got Whitelabel Error Page instead of helloWorld.html page
If you want Spring to scan for your controller then move your
MainController.javafromto
If you want Spring to scan for your controller, but if you want to keep your controller in the current package add this in your main class annotation
Both will yield the same result.
The first approach works the way that spring will start scanning all subpackages starting from the package where your main class is located
So if your main class is in
It will scan for all packages within it like
But it will not scan anything above it. That is just
controllerin your example