1.文件中引入
knife4j的依赖(若有swagger依赖最好删除)
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>
1.1删除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>
1.2 若springboot版本高无validation,需要加上依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
2.创建SwaggerConfiguration
.java配置文件,创建springfox提供的Docket分组对象
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfiguration {
@Bean
public Docket buildDocket() {
HashSet<String> strings = new HashSet<>();
strings.add("application/json");
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInfo())
//设置返回数据类型
.produces(strings)
//分组名称
.groupName("1.0")
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.chen"))
.paths(PathSelectors.any())
.build();
return docket;
}
private ApiInfo buildApiInfo() {
Contact contact = new Contact("chen的接口文档","","");
return new ApiInfoBuilder()
.title("chen的接口文档-管理API文档")
.description("管理文档api")
.contact(contact)
.version("1.0.0").build();
}
}
@EnableSwagger2 | 该注解是Springfox-swagger框架提供的使用Swagger注解,该注解必须加 |
@EnableKnife4j | 该注解是knife4j 提供的增强注解,Ui提供了例如动态参数、参数过滤、接口排序等增强功能,如果你想使用这些增强功能就必须加该注解,否则可以不用加 |
3.访问:localhost:端口号//doc.html