SWAGGER2 生成API文档

1.      在微服务中添加SWAGGER的依赖

在POM.XML中添加

<!-- Swagger -->

        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger2</artifactId>

        </dependency>

        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-swagger-ui</artifactId>

    </dependency>

2. 在启动类中添加SWAGGER注解@EnableSwagger2

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

@EnableDiscoveryClient

@EnableSwagger2

publicclassApplication  {

    publicstaticvoid main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

}

3.      添加SWAGGER配置类

@Configuration

publicclassSwaggerConfig {

    @Bean

    public Docket createRestApi(){

        returnnewDocket(DocumentationType.SWAGGER_2)

                .apiInfo(apiInfo())

                .select()

                .apis(RequestHandlerSelectors.basePackage("com.pengyu.demo"))

                .paths(PathSelectors.any())

                .build();

    }

    private ApiInfo apiInfo() {

        returnnew ApiInfoBuilder()

        .title("项目的名称")//此处添加项目的名称

        .description("项目描述")//项目描述

        .version("1.0版本版本")//版本

        .termsOfServiceUrl("NO terms of service")

        .license("The Apache License, Version 2.0")

        .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")

        .build();

        }     

}

4.      为API接口添加文档,注解说明

@Api() 
用于类;表示标识这个类是swagger的资源 
tags–表示说明 
value–也是说明,可以使用tags替代 
但是tags如果有多个值,会生成多个list

@Api(value="用户controller",tags={"用户操作接口"})

@RestController

public class UserController {

 

}

@ApiOperation() 用于方法;表示一个http请求的操作 
value
用于方法描述 
notes
用于提示内容 
tags
可以重新分组(视情况而用) 
@ApiParam() 
用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等) 
name–
参数名 
value–
参数说明 
required–
是否必填

@Api(value="用户controller",tags={"用户操作接口"})

@RestController

publicclass UserController {

     @ApiOperation(value="获取用户信息",tags={"获取用户信息copy"},notes="注意问题点")

     @GetMapping("/getUserInfo")

     public UsergetUserInfo(@ApiParam(name="id",value="用户id",required=true) Longid,@ApiParam(name="username",value="用户名") String username){

     // userService可忽略,是业务逻辑

      User user =userService.getUserInfo();

 

      return user;

  }

}


@ApiModel()用于类;表示对类进行说明,用于参数用实体类接收 
value–
表示对象名 
description–
描述 
都可省略 
@ApiModelProperty()
用于方法,字段;表示对model属性的说明或者数据操作更改 
value–
字段说明 
name–
重写属性名字 
dataType–
重写属性类型 
required–
是否必填 
example–
举例说明 
hidden–
隐藏

@ApiModel(value="user对象",description="用户对象user")

publicclassUserimplementsSerializable{

    privatestaticfinallongserialVersionUID = 1L;

     @ApiModelProperty(value="用户名",name="username",example="xingguo")

     private Stringusername;

     @ApiModelProperty(value="状态",name="state",required=true)

      private Integer state;

      private Stringpassword;

      private StringnickName;

      private IntegerisDeleted;

 

      @ApiModelProperty(value="id数组",hidden=true)

      private String[] ids;

      privateList<String> idList;

     //省略get/set

}

 

@ApiOperation("更改用户信息")

  @PostMapping("/updateUserInfo")

  publicintupdateUserInfo(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) User user){

 

     int num =userService.updateUserInfo(user);

     return num;

  }

@ApiIgnore()用于类或者方法上,可以不被swagger显示在页面上 
比较简单, 这里不做举例

@ApiImplicitParam() 用于方法 
表示单独的请求参数 
@ApiImplicitParams() 
用于方法,包含多个 @ApiImplicitParam 
name–
参数ming 
value–
参数说明 
dataType–
数据类型 
paramType–
参数类型 
example–
举例说明

  @ApiOperation("查询测试")

  @GetMapping("select")

  //@ApiImplicitParam(name="name",value="用户名",dataType="String", paramType = "query")

  @ApiImplicitParams({

  @ApiImplicitParam(name="name",value="用户名",dataType="string", paramType = "query",example="xingguo"),

  @ApiImplicitParam(name="id",value="用户id",dataType="long", paramType = "query")})

  public void select(){

  }

5.      在application.yml中添加地址,使得通过EUREKA能直接访问SWAGGER

eureka:

 client:

   serviceUrl:

     defaultZone:http://username:[email protected]:8099/eureka/

 instance:

   prefer-ip-address:  true

   lease-expiration-duration-in-seconds: 30

   lease-renewal-interval-in-seconds: 10

    status-page-url: http://${spring.cloud.client.ipAddress}:${server.port}/swagger-ui.html  #Eureka 中应用 INFO 页展示为 Swagger 生成的接口信息

猜你喜欢

转载自blog.csdn.net/xiaopengyu411/article/details/80338489