Spring Boot - Senseless 404 error

Asked

Viewed 947 times

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: Hierarquia das pastas do Projeto

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);
    }
}

2 answers

0

Put the main application class above the other packages. Switch to the Intellij Packages perspective and check if the class containing the annotation@SpringBootApplicationis at the root.

  • 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 method index() I put a System.out.println("entrou") just to test if the method is running and it was running yes, and tbm I tried to move the file index.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?

  • Add the annotation @ComponentScan(basePackageClasses = IndexController.class) in the main class.

  • Didn’t work either :(

0

To call the index, you need to put @Requestmapping("/").

@RequestMapping(value = "/")
public String index() {
    System.out.println("entrou");
    return "index";
}

Your URL will look like this: localhost:8080/<name_app_no_pom> Your hierarchy is correct and the boot class @Springbootapplication (if not generating .War) is also correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.