Swagger 枚举enum类型参数显示下拉框效果
枚举值
public enum ProductEnum { BOOK("书籍"), FOOD("食物"), OTHER("其他"); private String type; ProductEnum(String type) { this.type = type; } public String getType(){ return type; } public static ProductEnum fromType(String type){ for(ProductEnum typeEnum: values()){ if(typeEnum.type.equals(type)){ return typeEnum; } } return null; } }
控制器
@ApiImplicitParam(name = "type", paramType = "query", allowableValues = "书籍,食物") @GetMapping("/query") public Product queryByType(String type) { ProductEnum productEnum = ProductEnum.fromType(type); return new Product(49.9, "Think in Java", TypeEnum.BOOK); }
参数使用字符串,并且加上 swagger 注解allowableValues,取值与枚举定义一致。
然后在代码中,将字符串转换为枚举类型。
在 swagger UI 页面上的显示效果:


浙公网安备 33010602011771号