swagger全局修改接口信息


swagger全局配置

一、统一增加URL前缀

@Configuration
@EnableSwagger2
@Profile({"test","dev"})
public class Swagger2Config implements WebMvcConfigurer {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("com.xxx.controller"))
                .paths(PathSelectors.any())
                .build().pathMapping("/prefix/");  // 所有接口统一增加prefix前缀
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("XXX")
                .description("XXXX")
                .version("1.0")
                .build();
    }
}

二、统一增加参数

@Configuration
@EnableSwagger2
@Profile({"test","dev"})
public class Swagger2Config implements WebMvcConfigurer {

    @Bean
    public Docket createRestApi() {
        List<Parameter> parameter = new ArrayList<Parameter>();
        parameter.add(new ParameterBuilder().name("sign").description("签名").required(true).modelRef(new ModelRef("string")).parameterType("query").build());
        // 增加sign参数
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("com.xxxx.controller"))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(parameter);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("XXXX")
                .description("XXXX")
                .version("1.0")
                .build();
    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM