【问题标题】:No webpage was found for the web address: http://localhost:8080/ in Spring generated by codegen with swagger没有找到网页地址:http://localhost:8080/ in Spring由codegen用swagger生成
【发布时间】:2021-08-26 13:30:30
【问题描述】:

我正在使用 swagger 编辑器中的示例宠物商店学习 swagger:https://editor.swagger.io/

spring 的代码已生成,我没有更改任何内容。但是,每次我在 http://localhost:8080 上运行它时,它都会给出错误消息

This localhost page can’t be found
No webpage was found for the web address: http://localhost:8080/
HTTP ERROR 404

但我相信我应该在以下网站上看到类似的内容: https://petstore.swagger.io/ 请问如何解决这个问题?非常感谢。

我遵循控制器类 HomeController 中的 URL。

@Controller
public class HomeController {
    @RequestMapping(value = "/")
    public String index() {
        System.out.println("swagger-ui.html");
        return "redirect:swagger-ui.html";
    }
}

【问题讨论】:

    标签: spring swagger codegen


    【解决方案1】:

    您可能访问了错误的 URL。试试http://localhost:8080/<base-url>/swagger-ui.html

    此外,您可以参考此link 进行基本设置。

    【讨论】:

    • 基本网址是什么
    • 但在自述文件中:可以通过指向localhost:8080查看swagger-ui中的api文档
    • 我可以知道您尝试在哪个框架中构建您的服务器?基本 url 是您在应用程序中设置的 URI 的一部分,它是您的主机名之后的公共字符串,在您的情况下为 http://localhost:8080/
    • 在您的情况下,“/”网址直接重定向到“swagger-ui.html”。因此你可以使用http://localhost:8080/v2/
    【解决方案2】:

    上下文路径似乎是 v2。

    您应该可以在以下位置访问演示: http://localhost:8080/v2/<endoint>

    尝试访问: http://localhost:8080/v2/swagger-ui.html

    上下文路径在标题为application.properties 的配置文件中定义,该文件位于src/main/resource 下。该文件包含以下内容:

    springfox.documentation.swagger.v2.path=/api-docs
    server.contextPath=/v2
    server.port=8080
    spring.jackson.date-format=io.swagger.RFC3339DateFormat
    spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
    

    上下文路径在server.contextPath下定义。

    【讨论】:

    • 我现在可以打开了。但为什么?你怎么知道的?
    • 在 src/main/resource 下有一个名为 application.properties 的 spring 配置文件定义了基本 URL(技术上称为 contextPath)。我用文件的内容更新了我的答案。如果您在自己的环境中查找文件时遇到问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2018-03-31
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 2021-01-26
    相关资源
    最近更新 更多