Spring Suite - 控制器文件中的 @RequestMapping("/")、@GetMapping("/home"),导致 404 错误,localhost:8080 o/p of index.html

问题描述 投票:0回答:1

程序的结构是:

enter image description here

**我的员工档案是**

package model;

@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Employee {

    private String id;
    private String name;
    private String email;

}

我的控制器文件是:


@Controller
@RequestMapping("/welcome")
public class HomeController {
    
    @GetMapping("/home")
    public String getHome() {
        return "index";
    }
    

}

我的静态文件夹中有一个 html 文件,该文件是使用 localhost:8080 命令执行的,但是 localhost:8080/welcome/home 给出 404 not find 错误。

我期待 localhost:8080/welcome/home 给出 index.html 的 o/p,或者只是在浏览器上返回“index”。

应用程序文件正确执行,没有任何错误。

附注我是春季的绝对初学者,尝试仅使用控制器和

来实现一个非常好的程序

浏览器上的输出是一个简单的白色标签错误页面


spring controller
1个回答
0
投票

我假设您有一个 Spring Boot 应用程序,因此为了扫描所有包,有两种方法

首先。在主类中定义包

@SpringBootApplication
@ComponentScan({"model", "config", "controller", "service"})
public class MyAppApplication {

第二。将您的类移至主类的 SUBPACKAGE 中,这将帮助 Spring 自动扫描您的所有包。 所以如果你的主类位于

com.codemyth
这意味着您的其余软件包应该是:

com.codemyth.model
不是
model

com.codemyth.config
不是
config

com.codemyth.controller
不是
controller

com.codemyth.service
不是
service

我建议您使用第二种方法,该方法遵循如何在 Spring Boot 应用程序中构建包的标准。根据 Eclipse 的行为方式,我认为您只需左键单击配置包并将其拖放到

com.codemyth
包中,这会将其从配置重构为
com.codemyth.config
这就是您想要的。

© www.soinside.com 2019 - 2024. All rights reserved.