赞
踩
第一步:引入依赖
- <!-- swagger-->
- <dependency>
- <groupId>com.github.xiaoymin</groupId>
- <artifactId>knife4j-spring-boot-starter</artifactId>
- </dependency>
第二步:配置文件
- import com.sky.interceptor.JwtTokenAdminInterceptor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
- import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
- import springfox.documentation.builders.ApiInfoBuilder;
- import springfox.documentation.builders.PathSelectors;
- import springfox.documentation.builders.RequestHandlerSelectors;
- import springfox.documentation.service.ApiInfo;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
-
- /**
- * 配置类,注册web层相关组件
- */
- @Configuration
- @Slf4j
- public class WebMvcConfiguration extends WebMvcConfigurationSupport {
-
- @Autowired
- private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
-
- /**
- * 注册自定义拦截器
- *
- * @param registry
- */
- protected void addInterceptors(InterceptorRegistry registry) {
- log.info("开始注册自定义拦截器...");
- registry.addInterceptor(jwtTokenAdminInterceptor)
- .addPathPatterns("/admin/**")
- .excludePathPatterns("/admin/employee/login");
- }
-
-
- /**
- * 通过knife4j生成接口文档
- * @return
- */
- @Bean
- public Docket docket() {
- ApiInfo apiInfo = new ApiInfoBuilder()
- .title("项目接口文档名称")
- .version("2.0")
- .description("项目接口文档描述")
- .build();
- Docket docket = new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo)
- .select()
- //选择要扫描的Controller
- .apis(RequestHandlerSelectors.basePackage("com.sky.controller"))
- .paths(PathSelectors.any())
- .build();
- return docket;
- }
-
- /**
- * 设置静态资源映射
- * @param registry
- */
- protected void addResourceHandlers(ResourceHandlerRegistry registry) {
- registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
- registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
- }
- }
第三步:常用注解
第四步:进入localhost:8080/doc.html中进行接口调试
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。