【问题标题】:Spring.mvc.servlet.path vs Server.servlet.context-pathSpring.mvc.servlet.path 与 Server.servlet.context-path
【发布时间】:2020-06-25 10:28:08
【问题描述】:

我有 Spring Boot 应用程序,我在其中使用“spring.mvc.servlet.path=/api/v1”来访问我的端点。例如,当我在邮递员中测试我的“用户”端点时,我有:“http://localhost:8081/api/v1/users”。它有效。

问题是当我为我的文档集成 Swagger 时。我尝试使用“http://localhost:8081/api/v1/swagger-ui.html”在 swagger-u.html 中访问我的文档,但它不起作用。

当我将配置从 application.properties 更改为 "server.servlet.context-path=/api/v1" 时。 Swagger 的端点有效,而我的“http://localhost:8081/api/v1/users”无效。

关于如何解决此问题的任何建议?

【问题讨论】:

    标签: java spring swagger application.properties


    【解决方案1】:

    上下文路径与 servlet 路径?

    http://localhost:8080/context/myServlet

    如果您查看映射到路径 /myservlet 的 servlet 的请求 URL,这里 http:// 是协议,localhost 是服务器名称,/context 是上下文路径,/myServlet 是 servlet 路径。

    http://localhost:8080/context/myServlet/random

    如果您查看映射到路径 /myservlet/* 的 servlet 的请求 URL,这里 /myServlet 是 servlet 路径,/random 是额外路径信息...

    参考:https://coderanch.com/t/474573/certification/ContextPath-ServletPath

    spring boot 的 tomcat 只能提供该上下文路径下的内容

    server.servlet.contextPath=/api/

    Dispatcher servlet 路径允许您在其他路径上注册其他 servlet

    spring.mvc.servlet.path=/v1

    spring.mvc.servlet.path=/v2

    两个活跃的调度 servlet: http://localhost:8081/api/v1 和 http://localhost:8081/api/v2

    尝试访问 - http://localhost:8081/api/swagger-ui.html"

    对于开放式 API

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
    

    swagger-ui 位置已从 http://host/context-path/swagger-ui.html 到 http://host/context-path/swagger-ui/index.html 或 http://host/context-path/swagger-ui/

    简称。

    参考https://springfox.github.io/springfox/docs/snapshot/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 2018-07-20
      • 2023-03-14
      相关资源
      最近更新 更多