api在线生成接口文档-knife4j
作用:只需要在代码里引入依赖,加入注解即可实现自动生成在线api文档,就不需要手写api文档了
步骤一:添加依赖
<!--knife4j生成在线api文档工具-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
步骤二:添加配置类
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @param
* @author
* @date
* @description Knife4j生成在线api文件配置类
* @return
*/
@Configuration
@EnableOpenApi
@EnableWebMvc
@EnableSwagger2
public class Knife4jConfiguration {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("预约场馆")
.description("预约场馆")
.version("1.0")
.build())
.select()
//指定Controller扫描路径。可以不具体到controller,它会扫描指定路径下的所有
.apis(RequestHandlerSelectors.basePackage("com.banshan.auto"))
.paths(PathSelectors.any())
.build();
return docket;
}
//使用资源处理器注册静态资源
@SpringBootApplication
public class SwaggerBootstrapUiDemoApplication implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
}
步骤三:添加注解
@Api(tags = "AppointmentController-场馆预约")放在controller类上
@ApiOperation(value = "场馆预约分页查询")放在方法上
@ApiModel(description = "用户实体信息")放在实体类上
@ApiModelProperty("用户id")放在实体类字段上
@ApiParam(value = "电话号码",required = true)放在一个一个请求参数旁
参考文档:
knife4j官方文档
knife4j地使用
knife4j访问404
knife4j访问403