• Swagger2的配置


    Swagger2的配置:

    1.要用到的依赖包:

    1. <!-- swagger2 依赖 -->
    2. <dependency>
    3. <groupId>io.springfox</groupId>
    4. <artifactId>springfox-swagger2</artifactId>
    5. <version>2.7.0</version>
    6. </dependency>
    7. <!-- Swagger第三方ui依赖 -->
    8. <dependency>
    9. <groupId>com.github.xiaoymin</groupId>
    10. <artifactId>swagger-bootstrap-ui</artifactId>
    11. <version>1.9.6</version>
    12. </dependency>

    2.代码的实现Config类:

    1. package com.example.yebsever.config.swagger;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    5. import springfox.documentation.builders.ApiInfoBuilder;
    6. import springfox.documentation.builders.PathSelectors;
    7. import springfox.documentation.builders.RequestHandlerSelectors;
    8. import springfox.documentation.service.*;
    9. import springfox.documentation.spi.DocumentationType;
    10. import springfox.documentation.spi.service.contexts.SecurityContext;
    11. import springfox.documentation.spring.web.plugins.Docket;
    12. import springfox.documentation.swagger2.annotations.EnableSwagger2;
    13. import java.util.ArrayList;
    14. import java.util.List;
    15. /**
    16. * Swagger2配置
    17. */
    18. @Configuration
    19. @EnableWebMvc
    20. @EnableSwagger2
    21. public class Swagger2Config {
    22. @Bean
    23. public Docket createRestApi(){
    24. return new Docket(DocumentationType.SWAGGER_2)
    25. .apiInfo(apiInfo())
    26. .select()
    27. .apis(RequestHandlerSelectors.basePackage("com.example.yebsever.controller"))
    28. .paths(PathSelectors.any())
    29. .build();
    30. // .securityContexts(securityContexts())
    31. // .securitySchemes(securitySchemes());
    32. }
    33. private ApiInfo apiInfo(){
    34. return new ApiInfoBuilder()
    35. .title("云E办接口文档")
    36. .description("云E办接口文档")
    37. .contact(new Contact("xxxx","http:localhost:8088/doc.html","xxxx@xxxx.com"))
    38. .version("1.0")
    39. .build();
    40. }

    3.在security中放行Swagger的静态资源配置有时候会应为版本问题出现错误

    请按我的方法来写:在启动类加上该方法

    1. /**
    2. * 放行Swagger静态文件
    3. * @param registry
    4. */
    5. @Override
    6. public void addResourceHandlers(ResourceHandlerRegistry registry) {
    7. registry.addResourceHandler("/doc.html")
    8. .addResourceLocations("classpath:/META-INF/resources/");
    9. registry.addResourceHandler("/webjars/**").
    10. addResourceLocations("classpath:/META-INF/resources/webjars/");
    11. }
    1. package com.example.yebsever;
    2. import org.mybatis.spring.annotation.MapperScan;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. import org.springframework.scheduling.annotation.EnableScheduling;
    6. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
    7. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    8. @SpringBootApplication
    9. @MapperScan("com.example.yebsever.mapper")
    10. @EnableScheduling
    11. public class YebSeverApplication implements WebMvcConfigurer {
    12. /**
    13. * 放行Swagger静态文件
    14. * @param registry
    15. */
    16. @Override
    17. public void addResourceHandlers(ResourceHandlerRegistry registry) {
    18. registry.addResourceHandler("/doc.html")
    19. .addResourceLocations("classpath:/META-INF/resources/");
    20. registry.addResourceHandler("/webjars/**").
    21. addResourceLocations("classpath:/META-INF/resources/webjars/");
    22. }
    23. public static void main(String[] args) {
    24. SpringApplication.run(YebSeverApplication.class, args);
    25. System.out.println("文档接口:http://localhost:8088/doc.html");
    26. }
    27. }

    效果图:

    关注,收藏,点赞,有问题可以私信“门主” :v:z13135361785  

  • 相关阅读:
    解读 | 面向点云车辆检测的三维全卷积网络
    15-HBase的介绍、数据模型以及架构模型
    linux模块使用外部符号
    基础算法篇——快速排序
    app毕业设计开题报告基于Uniapp+SSM实现的Android的网店系统实现的App购物商城电商
    【算法|滑动窗口No.1】leetcode209. 长度最小的子数组
    通过代码解释什么是API,什么是SDK?
    AndroidStudio报错:Plugin with id ‘kotlin-android‘ not found.
    力扣:103. 二叉树的锯齿形层序遍历(Python3)
    牛客网刷题-(1)
  • 原文地址:https://blog.csdn.net/m0_55699184/article/details/133645577