美文网首页
Swagger2接口排序

Swagger2接口排序

作者: Kenny_Yu | 来源:发表于2020-06-11 21:50 被阅读0次

pom采用这个依赖,ui不能用2.9.2的。2.9.2的ui接口顺序不是根据后端排序显示的

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

配置文件为:

@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.kenny.enterpriser.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("XXXX后台")
                        .description("XXXXX后台")
                        .version("1.0")
                        .contact(new Contact("Kenny","blog.csdn.net","aaa@gmail.com"))
                        .license("The Apache License")
                        .licenseUrl("http://www.baidu.com")
                        .build());
    }

}

yml:

swagger:
  enable: true

想实现这种效果排序

@ApiOperation(value = "XXX)",position = 1)
@ApiOperation(value = "XXX)",position = 2)
@ApiOperation(value = "XXX)",position = 3)

则新建一个类增强ServiceModelToSwagger2MapperImpl,即可实现

@Component
@Primary
public class ServiceModelToSwagger2MapperImplExt extends ServiceModelToSwagger2MapperImpl {

    @Override
    protected Map<String, Path> mapApiListings(Multimap<String, ApiListing> apiListings) {
         Map<String, Path> paths = new LinkedHashMap<>();
        Multimap<String, ApiListing> apiListingMap = LinkedListMultimap.create();
        Iterator iter = apiListings.entries().iterator();
        while(iter.hasNext())
        {
            Map.Entry<String, ApiListing> entry = (Map.Entry<String, ApiListing>)iter.next();
            ApiListing apis =  entry.getValue();
            List<ApiDescription> apiDesc = apis.getApis();
            List<ApiDescription> newApi = new ArrayList<>();
            for(ApiDescription a:apiDesc){
                newApi.add(a);
            }
            Collections.sort(newApi, new Comparator<ApiDescription>() {
                @Override
                public int compare(ApiDescription left, ApiDescription right) {
                    return Integer.compare(left.getOperations().get(0).getPosition(),right.getOperations().get(0).getPosition());
                }
            });
            try {
                //因ApiListing的属性都是final故需要通过反射来修改值
                ModifyFinalUtils.modify(apis, "apis", newApi);
            } catch (Exception e) {
                e.printStackTrace();
            }
            apiListingMap.put(entry.getKey(),apis);
        }

        for (ApiListing each : apiListingMap.values()) {
            for (ApiDescription api : each.getApis()) {
                paths.put(api.getPath(), mapOperations(api, Optional.fromNullable(paths.get(api.getPath()))));
            }
        }
        return paths;
    }

    private Path mapOperations(ApiDescription api, Optional<Path> existingPath) {
        Path path = existingPath.or(new Path());
        for (springfox.documentation.service.Operation each : nullToEmptyList(api.getOperations())) {
            Operation operation = mapOperation(each);
            path.set(each.getMethod().toString().toLowerCase(), operation);
        }
        return path;
    }
}
public class ModifyFinalUtils {
    public static void modify(Object object, String fieldName, Object newFieldValue) throws Exception {
        Field field = object.getClass().getDeclaredField(fieldName);

        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true); //Field 的 modifiers 是私有的
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

        if(!field.isAccessible()) {
            field.setAccessible(true);
        }

        field.set(object, newFieldValue);
    }
}

相关文章

  • Swagger2接口排序

    pom采用这个依赖,ui不能用2.9.2的。2.9.2的ui接口顺序不是根据后端排序显示的 配置文件为: yml:...

  • 整合swagger2

    swagger2详解 [toc] 为啥使用swagger2 由于接口众多,调用者不懂如何使用接口参数,并且接口逻辑...

  • SpringBoot集成Swagger2

    pom.xml添加Swagger2依赖 创建Swagger2配置类 controller rest接口上添加 ...

  • Swagger2 Zuul 整合

    Swagger2 Swagger2是一个RESTful接口进行描述和展示的工具,可以通过 springfox-sw...

  • Swagger2的实战

    一、Swagger2的官方文档 Swagger官方地址 二、Swagger2的相关注解的介绍 1、接口相关的描述 ...

  • swagger2如何进行接口排序

    项目中使用的是springboot2.0+swagger2作为接口文档。遇到一个问题,如何对接口做排序让看接口的更...

  • SpringBoot集成Swagger2出现Unable to

    现象: SpringBoot集成Swagger2进行接口发布,SpringBoot版本:2.0.1,Swagger...

  • SpringBoot集成Swagger2在线文档

    SpringBoot集成Swagger2在线文档 前言 在开发RestFul接口时,我们常常需要自己写一 接口文档...

  • 推荐一个软件自动生成接口文档(带实现)

    Swagger2 上次给大家推荐Swagger2这个神器,自动生成接口文档。不需要自己再专门写文档,对于程序员来说...

  • Swagger2 集成 Oauth2

    为什么要给Swagger2集成Oauth2 保证接口安全 有些接口需要拥有登录状态(尽量保证接口无状态) LIVE...

网友评论

      本文标题:Swagger2接口排序

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