注意:

  如果你正在研究微服务,那必然少不了服务之间的相互调用,哪么服务之间的接口以及api就必须生成系统的管理文档了。如果你希望更好的管理你的API,你希望有一个工具能一站式地解决API相关的所有事情,那么,swagger将是一个不错的选择,以下就为大家介绍swagger是使用方法,如有不对之处,还望指正!

1、项目结构  springBoot-user-zuul-swagger — zuul网关 端口 9501   ls-prevention-check      — 服务1 端口 8091
  microcloud-provider-company — 服务2 端口 8105
2、实现放法  一、添加依赖
  分别在三个服务中添加Swagger需要依赖两个jar包,在pom.xml中添加如下坐标

     <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger2</artifactId>
             <version>2.7.0</version>
     </dependency>
     <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger-ui</artifactId>
             <version>2.7.0</version>
     </dependency>

  二、创建配置类    分别在服务提供者项目中加入Swagger一个配置类来进行对swagger的基本配置,

 @Configuration
 @EnableSwagger2
 public class SwaggerConfig {
 
     @Bean
     public Docket createRestApi() {
         return new Docket(DocumentationType.SWAGGER_2)
                 .apiInfo(apiInfo())
                 .select()
                 .apis(RequestHandlerSelectors.basePackage("com.zyc.controller"))
                 .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                 .paths(PathSelectors.any())
                 .build();
     }
 
     private ApiInfo apiInfo() {
         return new ApiInfoBuilder()
                 .title("Hello系统api")
                 .description("Hello系统接口文档说明")
                 .contact(new Contact("vker", "", "6492178@gmail.com"))
                 .version("1.0")
                 .build();
     }
 
     @Bean
     UiConfiguration uiConfig() {
         return new UiConfiguration(null, "list", "alpha", "schema",
                 UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
     }
 }

  三、启动类加注解    @EnableSwagger2  四、在需要生成的类或方法上加具体注解    

 @Api("Hello接口")
 @RestController
 @RequestMapping("/company")
 public class Hello {
     
     @ApiOperation(value="/get/hello方法")
     @GetMapping("/get/hello")
     public String hello() {
         return "Hello Zuul springBoot-microcloud-provider-company";
     }
     
 }

  注意:这里只是一个小栗子:详细注解含义如下:

 @Api() 用于类;表示标识这个类是swagger的资源 
 tags–表示说明 
 value–也是说明,可以使用tags替代 
 
 @ApiOperation() 用于方法;表示一个http请求的操作 
 value用于方法描述 
 notes用于提示内容 
 
 
 @ApiParam() 用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等) 
 name–参数名 
 value–参数说明 
 required–是否必填
 
 @ApiModel()用于类 ;表示对类进行说明,用于参数用实体类接收 
 value–表示对象名 
 
 @ApiModelProperty()用于方法,字段; 表示对model属性的说明或者数据操作更改 
 value–字段说明 
 name–重写属性名字 
 dataType–重写属性类型 
 required–是否必填 
 example–举例说明 
 hidden–隐藏
 
 @ApiImplicitParam() 用于方法 
 表示单独的请求参数
 
 @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam 
 name–参数ming 
 value–参数说明 
 dataType–数据类型 
 paramType–参数类型 
 example–举例说明
 
 @ApiIgnore
 作用于方法上,使用这个注解swagger将忽略这个接口

  五、重点:在springBoot-user-zuul-swagger项目中添加文档资源配置类 DocumentationConfig        注意:springBoot-user-zuul-swagger必须集成Zuul作为路由访问

 @Component
 @Primary
 public class DocumentationConfig implements SwaggerResourcesProvider{
 
     @Override
     public List<SwaggerResource> get() {
          List resources = new ArrayList<>();
             resources.add(swaggerResource("Hello接口", "/study-proxy/company-proxy/v2/api-docs", "1.0"));
             resources.add(swaggerResource("检查系统接口", "/study-proxy/prevention-check/v2/api-docs", "1.0"));
             return resources;
     }
 
      private SwaggerResource swaggerResource(String name, String location, String version) {
             SwaggerResource swaggerResource = new SwaggerResource();
             swaggerResource.setName(name);
             swaggerResource.setLocation(location);
             swaggerResource.setSwaggerVersion(version);
             return swaggerResource;
      }
 }

  六、在springBoot-user-zuul-swagger的application.yml中添加相关配置:  

 spring:
   application:
     name: springBoot-user-zuul
 zuul:
   prefix: /study-proxy
   ignored-services: "*"
   routes: 
     microcloud-provider-company: /company-proxy/**
     ls-prevention-check: /prevention-check/**

  七、访问http://localhost:9501/swagger-ui.html

图片描述