【问题标题】:Invalid swagger request path招摇请求路径无效
【发布时间】:2021-07-08 18:06:03
【问题描述】:

我正在使用 springdoc 并尝试在我的项目中配置 swagger。我有一个身份验证端点,想在 swagger ui 页面上执行请求。 Swagger 正确显示端点路径:/api/v1/auth,但是当我尝试在“试用”菜单中执行请求时,它会将请求发送到 http://localhost:8080/v3/api-docs/localhost/api/v1/auth

示例如下:

这是我在 swagger 中生成的 curl 连接字符串:

curl -X 'POST' \
  'http://localhost:8080/v3/api-docs/localhost/api/v1/auth' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "username": "string",
  "password": "string"
}'

还有配置bean:

@Configuration
public class SwaggerConfiguration {
    @Bean
    public GroupedOpenApi publicUserApi() {
        return GroupedOpenApi.builder()
                .group("Api")
                .pathsToMatch("/api/**")
                .build();
    }

    @Bean
    public OpenAPI customOpenApi() {
        return new OpenAPI()
                .info(new Info().title("Application API"))
                .servers(List.of(new Server().url("localhost").description("Server")));
    }
}

提前感谢您的帮助

【问题讨论】:

  • 您能否分享 Swagger-UI 中显示的服务器 URL 的屏幕截图?
  • @Debargha Roy,当然是screenshot

标签: java spring swagger springdoc


【解决方案1】:

问题是因为您将服务器 URL 设置为 localhost 而不是 http://localhost

当您指定不带协议的路径时,它始终被视为页面当前 URL 的相对路径,在本例中为 http://localhost:8080/v3/api-docs

更新您的 OpenAPI Bean 以反映带有协议的 URL 应该可以解决问题。

@Bean
public OpenAPI customOpenApi() {
    return new OpenAPI()
            .info(new Info().title("Application API"))
            .servers(List.of(new Server().url("http://localhost:8080").description("Server")));
}

为了清楚起见,我已将端口号明确指定为8080

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多