下面列出了怎么用springfox.documentation.spring.web.plugins.Docket的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 创建API
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(enabled)
// 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息)
.apiInfo(apiInfo())
// 设置哪些接口暴露给Swagger展示
.select()
// 扫描所有有注解的api,用这种方式更灵活
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
// 扫描指定包中的swagger注解
//.apis(RequestHandlerSelectors.basePackage("com.dimple.project.tool.swagger"))
// 扫描所有 .apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
/* 设置安全模式,swagger可以设置访问token */
.securitySchemes(securitySchemes())
.securityContexts(securityContexts())
.pathMapping(pathMapping);
}
/**
* Springfox configuration for the API Swagger docs.
*
* @param swaggerCustomizers Swagger customizers
* @param alternateTypeRules alternate type rules
* @return the Swagger Springfox configuration
*/
@Bean
@ConditionalOnMissingBean(name = "swaggerSpringfoxApiDocket")
public Docket swaggerSpringfoxApiDocket(List<SwaggerCustomizer> swaggerCustomizers,
ObjectProvider<AlternateTypeRule[]> alternateTypeRules) {
log.debug(STARTING_MESSAGE);
StopWatch watch = new StopWatch();
watch.start();
Docket docket = createDocket();
// Apply all SwaggerCustomizers orderly.
swaggerCustomizers.forEach(customizer -> customizer.customize(docket));
// Add all AlternateTypeRules if available in spring bean factory.
// Also you can add your rules in a customizer bean above.
Optional.ofNullable(alternateTypeRules.getIfAvailable()).ifPresent(docket::alternateTypeRules);
watch.stop();
log.debug(STARTED_MESSAGE, watch.getTotalTimeMillis());
return docket;
}
/**
* Will exposed on $HOST:$PORT/swagger-ui.html
*
* @return
*/
@Bean
public Docket apiDocumentation() {
return new Docket(SWAGGER_2)
.select()
.apis(basePackage("se.magnus.microservices.composite.product"))
.paths(PathSelectors.any())
.build()
.globalResponseMessage(POST, emptyList())
.globalResponseMessage(GET, emptyList())
.globalResponseMessage(DELETE, emptyList())
.apiInfo(new ApiInfo(
apiTitle,
apiDescription,
apiVersion,
apiTermsOfServiceUrl,
new Contact(apiContactName, apiContactUrl, apiContactEmail),
apiLicense,
apiLicenseUrl,
emptyList()
));
}
@Bean
public Docket userApi() {
List<ResponseMessage> list = new java.util.ArrayList<>();
list.add(new ResponseMessageBuilder().code(500).message("500 message").responseModel(new ModelRef("Result"))
.build());
list.add(new ResponseMessageBuilder().code(401).message("Unauthorized").responseModel(new ModelRef("Result"))
.build());
list.add(new ResponseMessageBuilder().code(406).message("Not Acceptable").responseModel(new ModelRef("Result"))
.build());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.tmobile.pacman"))
.paths(PathSelectors.any()).build()
.securitySchemes(chooseSecuritSchema())
.securityContexts(chooseSecurityContext())
.globalResponseMessage(RequestMethod.GET, list).globalResponseMessage(RequestMethod.POST, list);
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.ant("/api/v1/**"))
.build()
.apiInfo(
new ApiInfo(
apiTitle,
apiDescription,
apiVersion,
null,
null,
null,
null,
Collections.emptyList()
)
);
}
@Bean
public Docket createRestApi() {
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
tokenPar.name("Authorization").description("令牌").
modelRef(new ModelRef("string")).
parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// .apis(RequestHandlerSelectors.basePackage("com.open.capacity"))
.apis(RequestHandlerSelectors.any())
.paths( input ->PathSelectors.regex("/user.*").apply(input) || PathSelectors.regex("/permissions.*").apply(input)
|| PathSelectors.regex("/roles.*").apply(input) || PathSelectors.regex("/test.*").apply(input)
)
// .paths(PathSelectors.any())
.build().globalOperationParameters(pars);
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("org.zowe.apiml.apicatalog.controllers.api"))
.paths(
PathSelectors.any()
)
.build()
.securitySchemes(
Arrays.asList(
new BasicAuth("LoginBasicAuth"),
new ApiKey("CookieAuth", "apimlAuthenticationToken", "header")
)
)
.apiInfo(
new ApiInfo(
apiTitle,
apiDescription,
apiVersion,
null,
null,
null,
null,
Collections.emptyList()
)
);
}
@Bean
public Docket api() {
final ArrayList<Response> responseMessages = new ArrayList<>();
responseMessages.add(new ResponseBuilder()
.code("200")
.description("Successfully parsed the provided input")
.build());
responseMessages.add(new ResponseBuilder()
.code("503")
.description("Internal error, or Yauaa is currently still busy starting up.")
.build());
return new Docket(DocumentationType.SWAGGER_2)
.groupName("yauaa-v1")
.select()
.apis(withMethodAnnotation(ApiOperation.class))
.build()
.globalResponses(GET, responseMessages)
.globalResponses(POST, responseMessages)
.apiInfo(apiInfo());
}
@Bean
public Docket webApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("api")
//.host()
.genericModelSubstitutes(DeferredResult.class)
//.genericModelSubstitutes(ResponseEntity.class)
.useDefaultResponseMessages(false)
.forCodeGeneration(false)
.pathMapping("/")
.select()
//.paths(PathSelectors.regex("/api"))//过滤的接口
.build()
.apiInfo(webApiInfo())
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
@Bean
public Docket createRestApi() {
// 获取需要扫描的包
String[] basePackages = getBasePackages();
ApiSelectorBuilder apiSelectorBuilder = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select();
// 如果扫描的包为空,则默认扫描类上有@Api注解的类
if (ArrayUtils.isEmpty(basePackages)) {
apiSelectorBuilder.apis(RequestHandlerSelectors.withClassAnnotation(Api.class));
} else {
// 扫描指定的包
apiSelectorBuilder.apis(basePackage(basePackages));
}
Docket docket = apiSelectorBuilder.paths(PathSelectors.any())
.build()
.enable(swaggerProperties.isEnable())
.ignoredParameterTypes(ignoredParameterTypes)
.globalOperationParameters(getParameters());
return docket;
}
@Bean
public Docket createRestApi() {
//添加head参数start
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
tokenPar.name("Authorization").description("Token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
//添加head参数end
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.enilu.flash.api.controller"))
.paths(PathSelectors.any())
.build().globalOperationParameters(pars);
}
@Bean
public Docket createRestApi() {
ParameterBuilder tokenBuilder = new ParameterBuilder();
List<Parameter> parameterList = new ArrayList<>();
tokenBuilder.name("Authorization")
.defaultValue("去其他请求中获取heard中token参数")
.description("令牌")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(true).build();
parameterList.add(tokenBuilder.build());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(parameterList);
}
/**
* 创建RestApi 并包扫描controller
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//分组名称
// .groupName("2.X版本")
.select()
// // 对所有api进行监控
// .apis(RequestHandlerSelectors.any())
.apis(RequestHandlerSelectors.basePackage(basePackage))
//不显示错误的接口地址
.paths(PathSelectors.any())
//错误路径不监控
.paths(Predicates.not(PathSelectors.regex("/error.*")))
// .build();
.build()
.securityContexts(Lists.newArrayList(securityContext())).securitySchemes(Lists.<SecurityScheme>newArrayList(apiKey()));
}
@Bean
public Docket documentation() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket api(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.regex("(?!/error).+"))
.paths(PathSelectors.regex("(?!/actuator).+"))
.build();
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(this.getEnabled())
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(this.getWebBasePackage()))
.paths(not(regex("/error.*")))
.build();
}
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包下controller生成API文档
.apis(RequestHandlerSelectors.basePackage("com.macro.mall.tiny.controller"))
//为有@Api注解的Controller生成API文档
// .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
//为有@ApiOperation注解的方法生成API文档
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket customImplementation(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("org.openapitools.codegen.online.api"))
.build()
.forCodeGeneration(true)
.directModelSubstitute(java.time.LocalDate.class, java.sql.Date.class)
.directModelSubstitute(java.time.OffsetDateTime.class, java.util.Date.class)
.directModelSubstitute(JsonNode.class, java.lang.Object.class)
.ignoredParameterTypes(Resource.class)
.ignoredParameterTypes(InputStream.class)
.apiInfo(apiInfo());
}
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.macro.mall.demo"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket swaggerPersonApi10() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("cn.springcloud.sample.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(
new ApiInfoBuilder()
.version("1.0")
.title("Original Service API")
.description("Original Service API v1.0")
.build()
);
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.icedsoul.offlinemessageservice"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包下controller生成API文档
.apis(RequestHandlerSelectors.basePackage("com.macro.mall.tiny.controller"))
.paths(PathSelectors.any())
.build()
//添加登录认证
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage(BASE_PACKAGE))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket customImplementation(){
return new Docket(DocumentationType.SWAGGER_2)
.select()
.paths(or(regex("/api/v1.*")))
.build()
.apiInfo(apiInfo());
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(DEFAULT_API_INFO)
.produces(DEFAULT_PRODUCES_AND_CONSUMES)
.consumes(DEFAULT_PRODUCES_AND_CONSUMES);
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
.build()
//.directModelSubstitute(org.joda.time.LocalDate.class, java.sql.Date.class)
//.directModelSubstitute(org.joda.time.DateTime.class, java.util.Date.class)
.apiInfo(apiInfo());
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.blueskykong.lottor.server.controller"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.macro.mall.search.controller"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.massoudafrashteh.code.controller"))
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket postsApi() {
return new Docket(DocumentationType.SWAGGER_2).groupName("public-api")
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.automationintesting.api"))
.paths(PathSelectors.any())
.build();
}