【问题标题】:How to re-order endpoints on Swagger UI (OpenAPI)?如何在 Swagger UI (OpenAPI) 上重新排序端点?
【发布时间】:2021-03-05 18:56:38
【问题描述】:

我在我的 Spring Boot 应用程序中配置了 Swagger,Spring Boot 应用程序有 2 个控制器,caseController 和 AttachmentController,我希望我的附件控制器中的所有端点都出现在 case 控制器端点之后,但 swagger 正在做相反的事情。

招摇在做什么:

我的代码如下:

配置文件:

@Configuration
class OpenApiConfig(
    @Value("\${springdoc.info.title}") val title: String,
    @Value("\${springdoc.info.description}") val infoDescription: String,
    @Value("\${springdoc.info.version}") val version: String,
    @Value("\${springdoc.info.license.name}") val licenseName: String,
    @Value("\${springdoc.info.license.url}") val licenseUrl: String,
    @Value("\${springdoc.info.contact.email}") val email: String,
    @Value("\${springdoc.server.url}") val url: String,
    @Value("\${springdoc.server.description}") val description: String
) {
@Bean
fun customOpenAPIConfig(): OpenAPI {
    return OpenAPI()
            .components(
                    Components()
            )
            .info(
                    Info().title(title).description(infoDescription).version(version)
                            .license(License().name(licenseName).url(licenseUrl))
                            .contact(Contact().email(email))
            )
            .servers(listOf(Server().url(url).description(description)))
            .tags(listOf(Tag()))

 }
}

附件控制器:

@RestController
@RequestMapping("/cases/{id}/attachments")
lass AttachmentController(private val caseManagementService: CaseManagementService) {

@Operation(
    summary = "Add attachment",
    tags = ["attachment"],
    description = "Add attachment to a case"
)
@PostMapping()
@ResponseStatus(HttpStatus.CREATED)
fun addAttachment(@RequestBody attachmentRequest: AttachmentRequestDto, @PathVariable id: String, @RequestHeader(value = "X-API-KEY") xApiKey: String): Mono<AttachmentResponseDto> {

    ....
}

案例控制器:

@RestController
@RequestMapping("/cases")
class CaseController(private val caseManagementService: CaseManagementService) {

@Operation(
    summary = "Create case",
    tags = ["case"],
    description = "Create case"
)
@PostMapping()
@ResponseStatus(HttpStatus.CREATED)
fun createCase(@RequestBody caseRequest: CaseRequestDto, @RequestHeader(value = "X-API-KEY") xApiKey: String): Mono<CaseResponseDto> {

.... 
}

【问题讨论】:

  • 在我的应用程序中,我只是用数字命名我的端点。) 1. 案例 2. 附件。
  • 您需要添加注释/配置以在您的 OpenAPI 定义中生成顶级 tags 部分(参见第二个示例 here)并在该部分中放置 case 标签在Attachments之前。
  • 对不起@Helen 我应该在哪里添加注释/配置,有没有办法在我的代码中添加它?还是需要在 Swagger UI 中添加 int?
  • @Deepstack 你能给我一个例子吗,我不明白,你在哪里命名你的端点?
  • 标签顺序似乎由这条线控制:return OpenAPI() ... .tags(listOf(Tag()))。是否可以重写以按特定顺序添加标签,即先case,然后Attachments?或者,看看这是否有帮助 - How do I write a custom sorter to sort my springdoc tags by name in Swagger UI?

标签: java spring-boot kotlin swagger openapi


【解决方案1】:

请看我的回答here

简而言之,一种方法是使用@Tag 和属性springdoc.swagger-ui.tagsSorter=alpha 的组合

【讨论】:

    猜你喜欢
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多