gpt4 book ai didi

java - Spring Boot 2.1.7.RELEASE 中未提供静态内容

转载 作者:行者123 更新时间:2023-12-02 01:21:00 25 4
gpt4 key购买 nike

在我的 Springboot 应用程序中,即使从日志消息中可以看到其余端点被正确命中,我也会收到以下 404 错误。

我有一个带有以下主类的 springboot 应用程序:-

package com.springbootbasic;

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

@SpringBootApplication
public class SpringbootBasicApplication {

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

}

以下是 Controller 类:-

package com.springbootbasic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class HomeController {

Logger logger = LoggerFactory.getLogger(HomeController.class);

@RequestMapping("/hello")
public String home(Model model) {
logger.info("Rest endpoint /hello is being hit correctly.");
return "index.html";
}

@RequestMapping("/home")
@ResponseBody
public String response() {
logger.info("Rest endpoint /home is being hit correctly.");
return "Very well done the rest end point is working fine.";
}

}

以上两个类在同一个包中,并且HomeController是用@Controller注释的,而不是@RestController。

在 eclipse 的 src/main/public 源文件夹中,我创建了以下 index.html 文件。

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Welcome from Home</title>
</head>
<body>
<h2>Hello World!</h2>
</body>
</html>

当我点击 url http://localhost:8080/hello 时,在浏览器中我收到以下错误:-

There was an unexpected error (type=Not Found, status=404).
No message available

但是日志消息是:-

2019-08-28 20:22:38.953  INFO 20864 --- [nio-8080-exec-1] c.springbootbasic.HomeController   : Rest endpoint /hello is being hit correctly.
2019-08-28 20:22:52.179 INFO 20864 --- [nio-8080-exec-3] c.springbootbasic.HomeController : Rest endpoint /home is being hit correctly.
2019-08-28 20:25:20.858 INFO 20864 --- [nio-8080-exec-6] c.springbootbasic.HomeController : Rest endpoint /hello is being hit correctly.

如何解决这个问题?

最佳答案

问题出在您的模板名称上。您的模板名称是index,如果您点击http://localhost:8080/你可以看到你的页面。我也遇到了同样的问题,如果你重命名你的index.html 应该没问题。我不知道为什么会出现这种情况,可能与 spring-boot 嵌入式服务器有关。

编辑:来自https://spring.io/guides/gs/serving-web-content/

The index.html resource is special because it is used as a "welcome page" if it exists, which means it will be served up as the root resource, i.e. at http://localhost:8080/

希望这有帮助。

关于java - Spring Boot 2.1.7.RELEASE 中未提供静态内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57695263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com