pom导包
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
配置文件
@Configuartion //表明配置文件注解
public class Swagger 2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//basePackage后面路径,为扫描路径,在项目中的控制层controller
.apis(RequestHandlerSelectors.basePackage("com.fengmang.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
@Configuration @EnableSwagger2 public class Swagger2Config { /** * 创建API应用 * apiInfo() 增加API相关信息 * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现, * 指定扫描的包路径来定义指定要建立API的目录。 * @return */ @Bean public Docket coreApiConfig(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(adminApiInfo()) .groupName("adminApi") .select() //只显示admin下面的路径 .paths(Predicates.and(PathSelectors.regex("/admin/.*"))) .build(); } private ApiInfo adminApiInfo(){ return new ApiInfoBuilder() .title("标签") .description("用途描述") .version("1.0") .contact(new Contact("名字","路径","邮箱")) .build(); } }
启动类加
@EnableSwagger2 开启Swagger
@APi声名该controller啥玩意
@ApiOperation(给接口备注)
@ApiModelPraperty(实体类上给字段标注的
启动模块
http://localhost:8080/swagger-ui.html#/
端口号自己用到的模块中xml文件里的来