java springboot 初体验 (八)对接swagger2

  1. 上一篇
    1.   java springboot 初体验 (七)对接链路追踪
    2. https://www.cnblogs.com/zwjvzwj/p/16613085.html
  2. pom文件添加依赖包
    1.         <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
              <!-- 接口文档——Swagger2的使用 -->
              <dependency>
                  <groupId>io.springfox</groupId>
                  <artifactId>springfox-swagger2</artifactId>
                  <version>${spring-swagger2.version}</version>
              </dependency>
      
      
              <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
              <!-- 接口文档——Swagger2的使用 -->
              <dependency>
                  <groupId>io.springfox</groupId>
                  <artifactId>springfox-swagger-ui</artifactId>
                  <version>${spring-swagger-ui.version}</version>
              </dependency>

       

  3.   注册配置
    1. package com.zwj.zwjproject.configuration;
      
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import springfox.documentation.builders.ApiInfoBuilder;
      import springfox.documentation.builders.RequestHandlerSelectors;
      import springfox.documentation.service.ApiInfo;
      import springfox.documentation.spi.DocumentationType;
      import springfox.documentation.spring.web.plugins.Docket;
      
      /**
       * @ClassName: swagger2Configuration
       * @Author zhangwujie
       * @Date 2022/8/22 7:03 下午
       * @Description:
       */
      
      @Configuration //声明该类为配置类
      public class Swagger2Configuration {
          @Bean
          public Docket customDocket() {
              return new Docket(DocumentationType.SWAGGER_2)
                      .apiInfo(apiInfo())
                      .select()
                      .apis(RequestHandlerSelectors.basePackage("com.zwj.zwjproject.controller"))//扫描的包路径
                      .build();
          }
      
          private ApiInfo apiInfo() {
              return new ApiInfoBuilder()
                      .title("zwj-project 项目接口文档名称")//文档说明
                      .version("1.0.0")//文档版本说明
                      .build();
          }
      }

       

  4.    添加配置

    1.   在配置文件application.yml中或者apollo中国呢配置
      spring.mvc.pathmatch.matching-strategy=ant_path_matcher
  5.   启动服务
  6.  查看对外文档
    1. http://localhost:8101/swagger-ui.html
    2. 域名和端口按你的项目进行修改
  7.   下一篇
    1.   java springboot 初体验 (九)对接http请求
    2. https://www.cnblogs.com/zwjvzwj/p/16614205.html
posted @ 2022-08-22 20:21  zwjvzwj  阅读(74)  评论(0编辑  收藏  举报