swagger相关注解的使用
Swagger:Swagger中的常用注解_白色风车下的花海的博客-优快云博客_swagger常用注解
第一步:添加maven依赖
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
第二步:添加配置类(配置类中可以配置注入@Bean 注册多个docket:摘要的意思;通过扫描不同的包可以达到分类的效果 )
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
@EnableWebMvc
@EnableOpenApi
@EnableKnife4j
public class SwaggerConfig{
private ApiInfo apiInfo() {
Contact contact = new Contact("江雄", "https://www.baidu.com", "jx@qq.com");
return new ApiInfoBuilder()
.license("许可证(Aache license Version 3.0)")
.title("XX项目系统文档")
.description("描述信息")
.contact(contact)
.version("3.0")
.build();
}
@Bean
public Docket docket() {
return new Docket(DocumentationType.OAS_30)
//是否开启在线文档
.enable(true)
.select()
//扫描所有的包
//.apis(RequestHandlerSelectors.any())
//扫描存在 ApiOperation 的注解
//.apis(RequestHandlerSelectors.withClassAnnotation(ApiOperation.class))
//扫描指定包(可以用于分组)
.apis(RequestHandlerSelectors.basePackage("com.qf.jx.boot"))
//生产链接
.paths(PathSelectors.any())
.build()
//作者信息
.apiInfo(apiInfo())
.groupName("API接口集合")
.pathMapping("/");
}
}
第三步:直接访问 http://localhost:8080/swagger-ui/index.html
第三方框架整合swagger -------knife4j
第一步:添加maven依赖(如下:里面整合swagger)
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
第二步:添加配置类:和swagger配置文件一样:(如上)
第三步: 配置类实现WebMVCConfig接口:重写addResourceConfig方法:(如下)
import com.qf.jx.boot.springbootexercise1.interceptor.TestInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig 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/");
}
/* @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TestInterceptor())
//指定所有的请求都被拦截
.addPathPatterns("/**")
//指定放行的路径
.excludePathPatterns("/","/login");
}*/
}
第四步:直接访问 http://localhost:8080/doc.html