栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Swagger2为接口进行排序

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Swagger2为接口进行排序

声明!声明!声明!如果没有做其他配置的话,按照下方的配置肯定可以用,但是如果有其他配置的话我就不敢保证了,Swagger迭代的挺快的,不同版本之间的配置也略有不同。

第一步:引入依赖

注意:直接复制,不要修改版本


    io.springfox
    springfox-swagger2
    2.9.2

    com.github.xiaoymin
    swagger-bootstrap-ui
    1.9.6

    io.swagger
    swagger-annotations
    1.5.21



    io.swagger
    swagger-models
    1.5.21
第二步:写个swagger的配置类

注意:标红的地方自己修改

package com.example.demo.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlbasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.documentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;



@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(){
        return new Docket(documentationType.SWAGGER_2)
                //是否开启(true 开启,false()。生产环境建议隐藏)
                .apiInfo(apiInfo())
                .select().apis(RequestHandlerSelectors
                        .basePackage("com.example.demo.web"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //设置文档标题(API名称)
                .title("设备功能接口文档")
                //文档描述
                .description("接口说明")
                //服务条款URL
                .termsOfServiceUrl("http://localhost:8899/")
                //版本号
                .version("1.0")
                .build();
    }

    @Bean
    public CorsFilter corsConfig(){
        final UrlbasedCorsConfigurationSource urlbasedCorsConfigurationSource = new UrlbasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        //是否允许请求带有验证
        corsConfiguration.setAllowCredentials(true);
        //允许访问的客户端域名
        corsConfiguration.addAllowedOrigin("*");
        //允许服务端访问的客户端请求
        corsConfiguration.addAllowedHeader("*");
        //允许访问的方法名,GET,POST
        corsConfiguration.addAllowedMethod("*");
        urlbasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(urlbasedCorsConfigurationSource);
    }

}
第三步:看下图

打开swagger页面http://192.168.31.25:8899/doc.html

 勾选后保存

第四步:看下图

注意画红圈的两个地方,第一个ApiSort是用于为该模块排序,下面那个ApiOperationSupport是用于为该模块下的子接口进行排序。

 接口数量较多,且前端逻辑不太清晰的时候,建议搞开发的按照步骤,为每个接口进行排序,利人利己啊,要不然过个个把月,自己看接口都不知道先走哪个了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/306737.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号