有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring启动白标签错误页面(类型=未找到,状态=404)

下午好! 我正在开始春季研究,我以同样的方式遵循教程,但它返回一个错误:

enter image description here

文件夹结构:

enter image description here

奇怪的是: 如果我在br中插入“EventoController.java”。通用域名格式。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>

有人能告诉我哪里错了吗


共 (5) 个答案

  1. # 1 楼答案

    验证pom中是否有正确的thymeleaf依赖项。xml:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>      
    </dependency>
    
  2. # 2 楼答案

    确保主类位于根包中,高于其他类

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

    你的声明是这样的

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

    package br.com.SpringApp.SpringApp.controller; 控制器的名称,即EventoController&;索引控制器

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

    在这之后 清理项目并重新运行spring boot应用程序

  3. # 3 楼答案

    作为SpringBoot的初学者,任何人都可能至少发生过一次这种错误,因为有几个原因

    1. 应用程序类(例如:SpringAppApplication.java)应该在根级别,控制器类可以放在同一级别下,也可以放在子目录中。这个应用程序类应该用@SpringBootApplicationextends SpringBootApplication来表示
    2. 您可能会错过控制器类顶部的@RestController注释
    3. 或者可能会错过映射注释。(@PostMapping("/save")@GetMapping("/id")等)
    4. 最后,只需检查您是否输入了正确的请求映射地址(URL),您可能会错过斜杠或@RequestMapping("/customer")(如果控制器类中有注释)或URL中的拼写错误
  4. # 4 楼答案

    正如Deepak所回答的,主类应该位于根包中,高于其他包。 但如果你不想这样做,你可以使用:

    @SpringBootApplication(scanBasePackages = {"com.other.packages","com.other"})
    public class SpringAppApplication {
    .....
    }
    
  5. # 5 楼答案

    解决方案: 如果在控制器类上使用@Controller,那么它将被视为MVC控制器类。但是,如果您希望在RESTFul web服务中使用一个特殊的控制器,那么您可以使用@Controller@ResponseBody注释,或者直接在Controller类上使用@RestController。在使用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"; 
        }
    
    }