swagger介绍
在Swagger模块下添加依赖
创建Swagger的配置类
package com.qwl.manager.configuration;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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
public class SwaggerConfiguration {
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("controller")
.apiInfo(apiInfo())
.select()
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("HTTO API")
.description("管理端接口")
.termsOfServiceUrl("http://springfox.io")
.contact("qwl")
.license("Apache License Version 2.0")
.version("2.0")
.build();
}
}
Swagger优化
1.选择性显示接口
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("controller")
.apiInfo(apiInfo())
.select()
//通过包路径进行筛选
.apis(RequestHandlerSelectors.basePackage(ProductController.class.getPackage().getName()))
//根据URL进行筛选
.paths(PathSelectors.ant("/products/*"))
.build();
}
@Bean
public Docket defaultApi(){
return new Docket(DocumentationType.SWAGGER_2)
//设置一个默认的API,实现分组
.select().apis(RequestHandlerSelectors.basePackage(BasicErrorController.class.getPackage().getName()))
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("HTTO API")
.description("管理端接口")
.termsOfServiceUrl("http://springfox.io")
.contact("qwl")
.license("Apache License Version 2.0")
.version("2.0")
.build();
}
}
2.详细注释说明
3.中文显示
swagger模块
- @import、组合注解
- 使用spring.factories
- 使用@ConfigurationProperties
定义SwaggerInfo配置信息类
package com.qwl.swagger;
import org.springframework.stereotype.Component;
/**
* swagger配置信息
*/
@Component
public class SwaggerInfo {
private String groupName = "controller";
private String basePackage;
private String antPath;
private String title = "HTTP API";
private String description = "管理端接口";
private String license = "Apache License Version 2.0";
//set get
}
将manager模块的SwaggerConfiguration类移到swagger中并修改
package com.qwl.swagger;
@Configuration
@EnableSwagger2
@ComponentScan(basePackages = "com.qwl.swagger")
public class SwaggerConfiguration {
@Autowired
SwaggerInfo swaggerInfo;
@Bean
public Docket createRestApi(){
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName(swaggerInfo.getGroupName())
.apiInfo(apiInfo());
ApiSelectorBuilder builder =docket.select();
if(!StringUtils.isEmpty(swaggerInfo.getBasePackage())){
builder=builder.apis(RequestHandlerSelectors.basePackage(swaggerInfo.getBasePackage()));
}
if (!StringUtils.isEmpty(swaggerInfo.getAntPath())){
builder = builder.paths(PathSelectors.ant(swaggerInfo.getAntPath()));
}
return builder.build();
}
@Bean
public Docket defaultApi(){
return new Docket(DocumentationType.SWAGGER_2)
//设置一个默认的API,实现分组
.select().apis(RequestHandlerSelectors.basePackage(BasicErrorController.class.getPackage().getName()))
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title(swaggerInfo.getTitle())
.description(swaggerInfo.getDescription())
.termsOfServiceUrl("http://springfox.io")
.contact("qwl")
.license(swaggerInfo.getLicense())
.version("2.0")
.build();
}
}
让manager模块使用swagger的配置
第一种导入
第二种使用注解
建立EnableSwagger注解
package com.qwl.swagger;
import org.springframework.context.annotation.Import;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* 开启swagger文档自动生成功能
*/
@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(value = { java.lang.annotation.ElementType.TYPE })
@Documented
@Import(SwaggerConfiguration.class)
public @interface EnableMySwagger {
}
在manager中注入
组合注解
将SwaggerConfiguration上的@EnableSwagger2移到EnableSwagger上
第三种使用spring-factories
更新化配置