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