当前位置:   article > 正文

SpringBoot---------Swagger

SpringBoot---------Swagger

第一步:引入依赖

  1. <!-- swagger-->
  2. <dependency>
  3. <groupId>com.github.xiaoymin</groupId>
  4. <artifactId>knife4j-spring-boot-starter</artifactId>
  5. </dependency>

第二步:配置文件

  1. import com.sky.interceptor.JwtTokenAdminInterceptor;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  7. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
  9. import springfox.documentation.builders.ApiInfoBuilder;
  10. import springfox.documentation.builders.PathSelectors;
  11. import springfox.documentation.builders.RequestHandlerSelectors;
  12. import springfox.documentation.service.ApiInfo;
  13. import springfox.documentation.spi.DocumentationType;
  14. import springfox.documentation.spring.web.plugins.Docket;
  15. /**
  16. * 配置类,注册web层相关组件
  17. */
  18. @Configuration
  19. @Slf4j
  20. public class WebMvcConfiguration extends WebMvcConfigurationSupport {
  21. @Autowired
  22. private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
  23. /**
  24. * 注册自定义拦截器
  25. *
  26. * @param registry
  27. */
  28. protected void addInterceptors(InterceptorRegistry registry) {
  29. log.info("开始注册自定义拦截器...");
  30. registry.addInterceptor(jwtTokenAdminInterceptor)
  31. .addPathPatterns("/admin/**")
  32. .excludePathPatterns("/admin/employee/login");
  33. }
  34. /**
  35. * 通过knife4j生成接口文档
  36. * @return
  37. */
  38. @Bean
  39. public Docket docket() {
  40. ApiInfo apiInfo = new ApiInfoBuilder()
  41. .title("项目接口文档名称")
  42. .version("2.0")
  43. .description("项目接口文档描述")
  44. .build();
  45. Docket docket = new Docket(DocumentationType.SWAGGER_2)
  46. .apiInfo(apiInfo)
  47. .select()
  48. //选择要扫描的Controller
  49. .apis(RequestHandlerSelectors.basePackage("com.sky.controller"))
  50. .paths(PathSelectors.any())
  51. .build();
  52. return docket;
  53. }
  54. /**
  55. * 设置静态资源映射
  56. * @param registry
  57. */
  58. protected void addResourceHandlers(ResourceHandlerRegistry registry) {
  59. registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
  60. registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
  61. }
  62. }

第三步:常用注解

第四步:进入localhost:8080/doc.html中进行接口调试

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/545111
推荐阅读
相关标签
  

闽ICP备14008679号