美文网首页
SpringBoot+Swagger2+解析自定义枚举

SpringBoot+Swagger2+解析自定义枚举

作者: pingwazi | 来源:发表于2020-08-24 08:36 被阅读0次

一、导包

    <!--swagger  start-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
    </dependency>
    <!--swagger  end-->

二、编写配置类

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Value("${spring.application.name}")
    private String applicationName;
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包下controller生成API文档,这里可以通过指定注解的方式来进行扫描
                .apis(RequestHandlerSelectors.basePackage("com.pingwazi.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(applicationName)
                .description("接口文档")
                .version("1.0")
                .build();
    }
}

浏览器输入地址测试

http://localhost:8080/swagger-ui.html

常用注解

@Api(tags = "PmsBrandController") //标记在类上[controller类]
@ApiOperation("首页接口") //标记在方法上
@ApiModelProperty("状态码") //标记在字段上

image.png

支持自定义枚举

ResponseCode是一个自定义枚举,枚举中有两个字段(code、description),通过这种方式实现,最终展示给出来的就是[ 0/* 成功 /, -1/ 失败 */ ]

@Component
public class ResponseCodePropertyPlugin implements ModelPropertyBuilderPlugin {
    @Override
    public void apply(ModelPropertyContext context) {
        Optional<ApiModelProperty> annotation = Optional.absent();

        if (context.getAnnotatedElement().isPresent()) {
            annotation = annotation.or(ApiModelProperties.findApiModePropertyAnnotation(context.getAnnotatedElement().get()));
        }
        if (context.getBeanPropertyDefinition().isPresent()) {
            annotation = annotation.or(Annotations.findPropertyAnnotation(
                    context.getBeanPropertyDefinition().get(),
                    ApiModelProperty.class));
        }
        final Class<?> rawPrimaryType = context.getBeanPropertyDefinition().get().getRawPrimaryType();
        //过滤得到目标类型
        if (annotation.isPresent() && ResponseCode.class.isAssignableFrom(rawPrimaryType)) {
            //获取CodedEnum的code值
            ResponseCode[] values = (ResponseCode[]) rawPrimaryType.getEnumConstants();
            final List<String> displayValues = Arrays.stream(values).map(respCode -> Integer.toString(respCode.getCode())+"/* "+respCode.getDescpriotn()+" */").collect(Collectors.toList());
            final AllowableListValues allowableListValues = new AllowableListValues(displayValues, rawPrimaryType.getTypeName());
            //固定设置为int类型
            final ResolvedType resolvedType = context.getResolver().resolve(String.class);
            context.getBuilder().allowableValues(allowableListValues).type(resolvedType);
        }
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return true;
    }
}
@Getter
public enum ResponseCode {
    OK(0,"成功"),
    ERROR(-1,"失败");
    private int code;
    private String descpriotn;
    ResponseCode(int code,String descpriotn) {
        this.code = code;
        this.descpriotn=descpriotn;
    }
}
@Data
public class ApiData<T> implements Serializable {
    @ApiModelProperty("状态码")
    private ResponseCode statusCode;
    @ApiModelProperty("消息")
    private String msg;
    @ApiModelProperty("响应数据")
    private T data;

    public ApiData(ResponseCode statusCode, String msg, T data) {
        this.statusCode = statusCode;
        this.msg = msg;
        this.data = data;
    }

    public ApiData(ResponseCode statusCode, String msg) {
        this.statusCode = statusCode;
        this.msg = msg;
    }

    public ApiData() {
        this.statusCode=ResponseCode.OK;
        this.msg="成功";
    }
}
image.png

相关文章

  • SpringBoot+Swagger2+解析自定义枚举

    一、导包 二、编写配置类 浏览器输入地址测试 http://localhost:8080/swagger-ui.h...

  • MMPopupView的基本使用

    一 自定义界面 1 类型解析 MMPopupType是MMPopupView的一个枚举变量,主要用于动画效果 MM...

  • 枚举--java24(02/17/2016)

    如何自定义枚举类如何使用enum定义枚举类、枚举类的主要方法实现接口的枚举类 JDK1.5之前需要自定义枚举类JD...

  • 8.枚举和注解

    一、枚举 枚举的二种实现方式: 自定义类实现枚举 使用 enum 关键字实现枚举 自定义类实现枚举: 不需要提供s...

  • Swift 自定义Protocol、enum

    自定义protocol 自定义枚举

  • Chapter 8 . 枚举

    阅读原文 Chapter 8 . 枚举 8.1 枚举类 主要内容: 如何自定义枚举类 如何使用enum定义枚举类 ...

  • 枚举

    枚举数据自定义的数据类型(元组)枚举语法:enum 枚举名称{case 名称case 名称...}

  • 枚举类与注解

    1.自定义枚举类: 2.使用enum关键字定义枚举类:image.png 其余步骤与自定义枚举类一致

  • Java篇-枚举的使用

    一 : 自定义枚举类 枚举类调用 二 : 使用enum关键字定义枚举类 让枚举类实现接口: 可以让不同的枚举类的对...

  • go 枚举类型

    这里需要用到enum库 定义一个枚举类型 操作枚举enum 查看枚举值 修改自定义枚举值 添加和移除枚举值

网友评论

      本文标题:SpringBoot+Swagger2+解析自定义枚举

      本文链接:https://www.haomeiwen.com/subject/xwugjktx.html