swagger2的分组配置
程序员文章站
2022-07-07 18:27:59
...
当微服务的服务越来越多swagger2展示的越来越长这时候需要进行分页展示
swagger2头上有下拉框可以groupName进行分组展示
java代码代码:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.globalOperationParameters(operationParameters())
.groupName("第一个接口服务页面")
.genericModelSubstitutes(DeferredResult.class)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.tan.oauth2.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(" createRestApi Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多Spring Boot相关文章请关注:http://blog.didispace.com/")
.termsOfServiceUrl("http://github.com/")
.contact("程序猿痛苦")
.version("1.0")
.build();
}
@Bean
public Docket openRestApi() {
Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
@Override
public boolean apply(RequestHandler input) {
// Class<?> declaringClass = input.declaringClass();
// if (declaringClass == BasicErrorController.class)// 排除
// return false;
// if(declaringClass.isAnnotationPresent(ApiOperation.class)) // 被注解的类
// return true;
// if(input.isAnnotatedWith(ResponseBody.class)) // 被注解的方法
// return true;
// if (input.isAnnotatedWith(ApiOperation.class))//只有添加了ApiOperation注解的method才在API中显示
// return true;
if (input.isAnnotatedWith(ApiOperation.class))//只有添加了ApiOperation注解的method才在API中显示
return true;
return false;
}
};
return new Docket(DocumentationType.SWAGGER_2)
.groupName("第二个接口服务页面")
.genericModelSubstitutes(DeferredResult.class)
.apiInfo(openapiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.tan.oauth2.controller2"))
// .apis(predicate)
.paths(PathSelectors.any())
.build();
}
private ApiInfo openapiInfo() {
return new ApiInfoBuilder()
.title("openRestApi Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多Spring Boot相关文章请关注:http://blog.didispace.com/")
.termsOfServiceUrl("http://github.com/")
.contact("程序猿痛苦")
.version("1.0")
.build();
}
public List<Parameter> operationParameters() {
//添加head参数start
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
tokenPar.name("x-access-token")
.description("令牌")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build();
pars.add(tokenPar.build());
//添加head参数end
return pars;
}
}
pom.xml文件配置
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
swagger2头上有下拉框可以groupName进行分组展示
java代码代码:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.globalOperationParameters(operationParameters())
.groupName("第一个接口服务页面")
.genericModelSubstitutes(DeferredResult.class)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.tan.oauth2.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(" createRestApi Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多Spring Boot相关文章请关注:http://blog.didispace.com/")
.termsOfServiceUrl("http://github.com/")
.contact("程序猿痛苦")
.version("1.0")
.build();
}
@Bean
public Docket openRestApi() {
Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
@Override
public boolean apply(RequestHandler input) {
// Class<?> declaringClass = input.declaringClass();
// if (declaringClass == BasicErrorController.class)// 排除
// return false;
// if(declaringClass.isAnnotationPresent(ApiOperation.class)) // 被注解的类
// return true;
// if(input.isAnnotatedWith(ResponseBody.class)) // 被注解的方法
// return true;
// if (input.isAnnotatedWith(ApiOperation.class))//只有添加了ApiOperation注解的method才在API中显示
// return true;
if (input.isAnnotatedWith(ApiOperation.class))//只有添加了ApiOperation注解的method才在API中显示
return true;
return false;
}
};
return new Docket(DocumentationType.SWAGGER_2)
.groupName("第二个接口服务页面")
.genericModelSubstitutes(DeferredResult.class)
.apiInfo(openapiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.tan.oauth2.controller2"))
// .apis(predicate)
.paths(PathSelectors.any())
.build();
}
private ApiInfo openapiInfo() {
return new ApiInfoBuilder()
.title("openRestApi Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多Spring Boot相关文章请关注:http://blog.didispace.com/")
.termsOfServiceUrl("http://github.com/")
.contact("程序猿痛苦")
.version("1.0")
.build();
}
public List<Parameter> operationParameters() {
//添加head参数start
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
tokenPar.name("x-access-token")
.description("令牌")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build();
pars.add(tokenPar.build());
//添加head参数end
return pars;
}
}
pom.xml文件配置
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
上一篇: MySQL函数大全及用法示例
下一篇: Hack技术总结(二)之浏览器私有前缀
推荐阅读
-
Spring Boot 配置 IDEA和DevTools 热部署的方法
-
vue webpack开发访问后台接口全局配置的方法
-
win10jdk环境变量配置过程(配置jdk的环境变量的步骤)
-
win10免安装版本的MySQL安装配置教程
-
Vue基于vuex、axios拦截器实现loading效果及axios的安装配置
-
新浪SAE云平台下使用codeigniter的数据库配置
-
在Windows平台的Apache中配置Python
-
Spring的配置,XML提示的配置,Bean的相关配置
-
修改apache配置文件去除thinkphp url中的index.php
-
JSP Servelet 数据源连接池的配置