Spring Boot Whitelabel 错误页面(type=Not Found,status=404)

新手上路,请多包涵

下午好!我正在开始春季学习,我正在按照相同的方式学习教程,但它返回错误:

在此处输入图像描述

文件夹结构:

在此处输入图像描述

奇怪的是:如果我将“EventoController.java”插入到 br.com.SpringApp.SpringApp 中,它会正常工作。

 package br.com.SpringApp.SpringApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringAppApplication.class, args);
    }
}

.

 package br.com.SpringApp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class EventoController {

    @RequestMapping("/cadastroEvento")
    public String form() {
        return "evento/formEvento";
    }

}

根据要求,我添加了 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
   <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>br.com.SpringApp</groupId>
    <artifactId>SpringApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringApp</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

   </project>

有人可以告诉我哪里错了吗?

原文由 sounobre 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 659
2 个回答

确保您的主类位于其他类之上的根包中。

当您运行 Spring Boot 应用程序(即用@SpringBootApplication 注释的类)时,Spring 将只扫描主类包下的类。

所以你的声明是这样的

package br.com.SpringApp.SpringApp; 在这个主类中,即 SpringAppApplication

package br.com.SpringApp.SpringApp.controller; 你的控制器的名称,即 EventoController 和 indexControllers

package br.com.SpringApp.SpringApp.model; 您的模型名称,即 Evento

在此之后清理您的项目并重新运行 spring boot 应用程序;

原文由 Dipak Thoke 发布,翻译遵循 CC BY-SA 3.0 许可协议

解决方案:如果您在 Controller 类上使用 @Controller ,那么它将被视为 MVC 控制器类。但是,如果您想要在 RESTFul Web 服务中使用一个特殊的控制器,那么您可以使用 @Controller 以及 @ResponseBody 注释,或者您可以直接使用 @RestController Controller 类。它对我有用,因为我在使用 RestFul web 服务创建 SpringBoot 项目时遇到了同样的错误。

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

}

或者:

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class EventoController {

    @RequestMapping("/cadastroEvento")
    public String form() {
        return "evento/formEvento";
    }

}

原文由 Mitali 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题