1、常用注解
2、配置依赖,在pom.xml <dependencies></dependencies> 内添加如下内容
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
3、写Swagger2的配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* @Description:swagger2的配置文件
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select() //按条件生成文档
.apis(RequestHandlerSelectors.any()) //设置生成api文档的范围
//.apis(RequestHandlerSelectors
// .basePackage("com.jc.demo1.controller")) //设置生成api文档的包
.paths(PathSelectors.any()).build();
}
/**
* @Description: 构建 api文档的信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 设置页面标题
.title("这是页面标题")
// 设置联系人
.contact(new Contact("咸鱼str", "https://me.csdn.net/weixin_40373925", "1527295607@qq.com"))
// 描述
.description("描述信息")
// 定义版本号
.version("1.0")
.build();
}
}
4、查看效果