0

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:

  1. Project: Maven
  2. Language: Java
  3. Spring Boot: 3.1.2
  4. Dependencies: Spring Web

Clicked on Generate to download the starter app, extracted it and added:

  1. helloWorld.html- this just diplays Hello World! (Please see below for the GitHub Repo URL)
  2. 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

Amar
  • 3
  • 3

1 Answers1

0

If you want Spring to scan for your controller then move your MainController.java from

package controller;

to

package com.example.HelloWorldWebApp.controller;

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

@SpringBootApplication(scanBasePackages = "controller")

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

package com.example.HelloWorldWebApp;

It will scan for all packages within it like

package com.example.HelloWorldWebApp.controller;
package com.example.HelloWorldWebApp.service;
package com.example.HelloWorldWebApp.repo;

But it will not scan anything above it. That is just controller in your example

zawarudo
  • 1,907
  • 2
  • 10
  • 20
  • Thank you for the two solutions and the explanation. Moving the controller class to a child package of the main class's package resolved the issue. – Amar Aug 15 '23 at 21:27
  • Glad I could help, have a great day Amar – zawarudo Aug 15 '23 at 22:20