• 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  

  • 相关阅读:
    java开发工具IntelliJ IDEA使用教程:检查项目状态
    Mybatis的mapper.xml批量插入、修改sql
    面试资料快速复习 Git常用命令(简单实用)
    人工神经网络技术的优点,浅谈人工神经网络
    【Spring Cloud实战】SpringCloud Sleuth分布式请求链路跟踪
    eBPF 入门实践教程(一):编写 eBPF 程序监控打开文件路径并使用 Prometheus 可视化
    深度学习Tensorflow: CUDA_ERROR_OUT_OF_MEMORY解决办法
    国内领先的五大API接口供应商
    Oracle数据库迁移- UID不一致导致的种种问题
    Anchor-free目标检测综述 -- Dense Prediction篇
  • 原文地址:https://blog.csdn.net/m0_55699184/article/details/133645577