1
I’m starting with Spring boot and created a project by Spring Initializr with Spring Boot 2.0.5
and dependencies Web, PostgreSQL e DevTools
, I imported the project in Intellij, until then everything right, then I followed some tutorials I saw on the internet and created a package in the same place where the class with the @SpringBootApplication
called controller
and inside I created a class TesteController
, with the following code:
package com.projetospringboot.meuprojeto.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class IndexController {
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String index() {
System.out.println("entrou");
return "index";
}
}
and then in the folder templates
i created an html file called index.html
and ran the project, according to the tutorials I saw and the logic if I opened the browser and accessed localhost:8080/home
it should show me the html I did but it returns an error saying:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this
as a fallback.
Mon Sep 17 17:37:48 BRT 2018
There was an unexpected error (type=Not Found, status=404).
No message available
and what I understood from this error is that it nn has a mapped route to /error
and this I see how to solve later because what he means is that it gave a 404 error and how nn has a route to /error
He showed it, the question is, why is he giving you 404 error?
I tried to change the return
of the method index()
for all kinds of routes "./index", "../index", "templates/index", "./templates/index", "index.html", "./index.html"
and etc.. but none worked.
here is a print of the hierarchy of project folders:
the class code with main method:
package com.projetospringboot.meuprojeto;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({"com.projetospringboot.meuprojeto.controller"})
public class MeuProjetoApplication {
public static void main(String[] args) {
SpringApplication.run(MeuProjetoApplication.class, args);
}
}
About the main class, the class with the
@SpringBootApplication
, be above the others, int, that’s not my problem because I did a test on that methodindex()
I put aSystem.out.println("entrou")
just to test if the method is running and it was running yes, and tbm I tried to move the fileindex.html
for my controller package, i.e., below the class q contains the@SpringBootApplication
and even then gave the same mistake, which will be q can be?– Bruno
Add the annotation
@ComponentScan(basePackageClasses = IndexController.class)
in the main class.– renanvm
Didn’t work either :(
– Bruno