【问题标题】:Spring Boot OpenAPI 3 with Spring Data REST带有 Spring Data REST 的 Spring Boot OpenAPI 3
【发布时间】:2022-01-21 11:30:06
【问题描述】:

我无法使用 OpenAPI 记录我的 Spring Data REST API。 swagger-ui 的主页(显然还有 /v3/api-docs)中没有显示任何内容。

这是我的依赖项的摘录:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.6.4</version>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-data-rest</artifactId>
        <version>1.6.4</version>
    </dependency>

还有我的 JPA 存储库:

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonsRepository extends JpaRepository<Person, Long> {
Person findByLastname(@Param("name") @RequestParam("name") String lastname);
}

这是我的 Spring Boot 设置:

spring.datasource.url=jdbc:h2:mem:testdb;DATABASE_TO_UPPER=false
spring.jpa.defer-datasource-initialization=true
spring.jpa.open-in-view=false
management.endpoints.web.exposure.include=*
springdoc.swagger-ui.operationsSorter=method
#springdoc.paths-to-match=/people/**

当然,我的 CRUD API 在 /people PATH 上是可以的。 甚至 /profile/people PATH 似乎也是正确的。

我一定是遗漏了什么...感谢您的帮助。

【问题讨论】:

    标签: spring-boot spring-data-jpa spring-data-rest springdoc springdoc-ui


    【解决方案1】:

    我希望你需要试试这个网址。

    http://localhost:8080/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config#

    我的配置

    另外,我创建了一个 OpenAPI bean

     @Bean
        public OpenAPI customOpenAPI() {
            return new OpenAPI()
                    .components(new Components())
                    .info(new Info().title("Test")
                            .description("Test Description")
                            .version("1.0.0"));
        }
    

    RestController API 端点方法之上 ,我添加了以下注释

     @Operation(summary = "Send Messages to Ibm Mq", description = "Send Message to the related ibm mq")
        @ApiResponses(value = {
                @ApiResponse(responseCode = "200", description = "Success Response",
                        content = @Content(schema = @Schema(implementation = SomeClass.class), mediaType = "application/json")),
                @ApiResponse(responseCode = "500", description = "Internal System Error",
                        content = @Content(schema = @Schema(implementation = SomeClass.class), mediaType = "application/json")),
                @ApiResponse(responseCode = "400", description = "Invalid Parameter Request",
                        content = @Content(schema = @Schema(implementation = SomeClass.class), mediaType = "application/json"))
        })
    

    所有进口来自 io.swagger.v3.oas.annotations 包

    【讨论】:

    • 非常感谢您的提示!但是,这是否记录在任何地方?我已经尝试对属性进行硬编码(springdoc.swagger-ui.configUrl=/v3/api-docs/swagger-config#),但最终得到了一个宠物商店演示......
    • 惊喜:在丢弃上述属性后,主 URL (localhost:8080/swagger-ui/index.html) 终于开始显示正确的 API 文档...我现在很困惑:-D
    猜你喜欢
    • 2016-04-17
    • 2021-03-29
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2019-01-23
    • 2021-11-11
    • 2021-12-21
    • 2022-12-06
    相关资源
    最近更新 更多