Spring Boot Whitelabel Error page (type=Not Found, status=404)

前端 未结 5 2281
离开以前
离开以前 2021-02-19 10:31

Good afternoon! I\'m starting spring studies, I\'m following a tutorial the same way, but it returns an error:

Folder structure:

The strange th

相关标签:
5条回答
  • 2021-02-19 10:43

    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 {
    .....
    }
    
    0 讨论(0)
  • 2021-02-19 10:53

    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>
    
    0 讨论(0)
  • 2021-02-19 10:55

    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;

    0 讨论(0)
  • 2021-02-19 11:02

    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"; 
        }
    
    }
    
    0 讨论(0)
  • 2021-02-19 11:04

    As a beginner to the SpringBoot, this error can be happened to anyone at least one time, because for a few reasons.

    1. 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 @SpringBootApplication or extends SpringBootApplication
    2. You may miss @RestController annotation at top of the controller class.
    3. Or can be missed the mapping annotation.( @PostMapping("/save") or @GetMapping("/id") etc.)
    4. 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.
    0 讨论(0)
提交回复
热议问题