通常我们进行前后端分离项目实现的时候,需要前后端对应接口,然而写文档又太过麻烦。。编不出来了,直接上代码,首先我们在我们的maven中添加
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
然后编辑一个配置类
@Configuration //配置注解类
@EnableSwagger2//开启swagger
public class SwaggerConfig {
//获取生产环境是否激活swagger
@Value("${swagger.show}")
private boolean swaggerShow;
//多个Docket 就有多个分组
@Bean
public Docket createRestApi2() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(swaggerShow)
.apiInfo(apiInfo())
//.groupName("yeyeye") 选中分组API
.groupName("yeyeye22")
// .groupName("yyy1")
//.enable(true)是否启用swagger 默认为true 如果改成false将显示不了
.enable(true)
.select()
// .apis(RequestHandlerSelectors.any())
//paths() 过滤什么路径
.paths(PathSelectors.any())
.build();
}
@Bean
public Docket createRestApi1() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(swaggerShow)
.apiInfo(apiInfo())
.groupName("yeyeye111")
// .groupName("yyy1")
//.enable(true)是否启用swagger 默认为true 如果改成false将显示不了
.enable(true)
.select()
.apis(RequestHandlerSelectors.any())
//paths() 过滤什么路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("yeye","www.baidu.com","2698105@qq.com");
return new ApiInfo("yeyeye开发文档","小白上路","V1.0","www.baidu.com",contact,"www.baidu.com","www.baidu.com"
);
}
}
然后就可以了,更加详细的可去官网查看
https://swagger.io/docs/specification/2-0/basic-structure/