首页 > 解决方案 > Swagger 枚举值没有显示在下拉列表中?

问题描述

这是我接受枚举的控制器方法

@GetMapping("/{sortBy}/{page}/{size}")
    public ResponseDto<ReviewsResponseDto, Void> searchAllReviews(
            @PathVariable(value = "sortBy") ReviewsSortBy sortBy,
            @PathVariable Integer page, @PathVariable Integer size) 

这是我的枚举 ReviewsSortBy

@Getter
@ToString
public enum ReviewsSortBy {
      DEFAULT,
      FEATURED,
      RECENT;
}

现在的问题是,swagger 不会在下拉列表中显示枚举的可能值,而是仅将“ReviewsSortBy()”显示为相同下拉列表中的唯一可选值。

注意:Swagger 版本 2.9.2

这是相同的屏幕截图。 枚举的招摇下拉问题

标签: javaspring-bootswaggerswagger-uiswagger-2.0

解决方案


看起来您正在使用带有 springfox 的 springboot。我有一个类似的问题,但不是相同的问题。使用 Spring 的请求映射注释的属性显式指定允许的内容类型consumes会导致 Swagger 中的枚举下拉字段成为文本框。

文本输入

  @PostMapping(value = "/items/{uuid}/images",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  @PreAuthorize(Scopes.PUBLISH)
  public ImageMetadataDTO upload(@PathVariable @Uuid String 
      @RequestParam(value = "graphicType") GraphicType 

枚举下拉菜单

  @PostMapping(value = "/items/{uuid}/images",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  @PreAuthorize(Scopes.PUBLISH)
  public ImageMetadataDTO upload(@PathVariable @Uuid String 
      @RequestParam(value = "graphicType") GraphicType 

推荐阅读