Swagger和knife4j

Swagger和knife4j

1-swagger

1.1-pom.xml依赖

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

1.2-配置类SwaggerConfiguration.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

   @Bean
   public Docket buildDocket() {
      return new Docket(DocumentationType.SWAGGER_2)
              .apiInfo(apiInfo())
              .select()
              //为当前包下controller生成API文档
              .apis(RequestHandlerSelectors.basePackage("cn.ztuo.swagger"))
              //为有@Api注解的Controller生成API文档
//                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
              //为有@ApiOperation注解的方法生成API文档
//                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
              .paths(PathSelectors.any())
              .build();
   }

   private ApiInfo apiInfo() {
      Contact contact = new Contact("ztuo","","");
      return new ApiInfoBuilder()
              .title("SwaggerUI演示")
              .description("SwaggerUI测试")
              .contact(contact)
              .version("1.0.0").build();
   }
}

1.3-数据准备Person.java

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class Person {
    @ApiModelProperty("person-id")
    private String id;
    @ApiModelProperty("person-name")
    private String name;
    @ApiModelProperty("person-age")
    private int age;
    @ApiModelProperty("person-address")
    private String address;
}

1.4-数据准备User.java

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class User {
    @ApiModelProperty(value="姓名",required = true)
    private String name;
    @ApiModelProperty(value="年龄",required = true)
    private String age;
}

1.5-测试

启动admin微服务,访问地址:http://host:port/swagger-ui.html

package cn.ztuo.swagger.controller;

import cn.ztuo.swagger.pojo.Person;
import cn.ztuo.swagger.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@Api(value="swagger-test",tags="channer",description = "swagger测试Api")
@RestController
@RequestMapping("/swagger")
public class SwaggerController {

    @ApiOperation("访问person测试")
    @GetMapping("/person")
    public Person test(){
        Person p=new Person();
        p.setId("001");
        p.setAddress("紫荆城");
        p.setName("老八");
        p.setAge(32);
        return p;
    }

    @ApiOperation("带参访问person测试")
    @PostMapping("/person2")
    public Person test1(@RequestBody User user){
        System.out.println(user);
        Person p=new Person();
        p.setId("002");
        p.setAddress("驻马店");
        p.setName("六六六");
        p.setAge(32);
        return p;
    }
}

2-Swagger常用注解

在Java类中添加Swagger的注解即可生成Swagger接口文档,常用Swagger注解如下:

@Api:修饰整个类,描述Controller的作用 @ApiOperation:描述一个类的一个方法,或者说一个接口 @ApiParam:单个参数的描述信息

@ApiModel:用对象来接收参数

@ApiModelProperty:用对象接收参数时,描述对象的一个字段

@ApiResponse:HTTP响应其中1个描述

@ApiResponses:HTTP响应整体描述

@ApiIgnore:使用该注解忽略这个API

@ApiError :发生错误返回的信息

@ApiImplicitParam:一个请求参数

@ApiImplicitParams:多个请求参数的描述信息

@ApiImplicitParam属性:

属性 取值 作用
paramType 查询参数类型
path 以地址的形式提交数据
query 直接跟参数完成自动映射赋值
body 以流的形式提交 仅支持POST
header 参数在request headers 里边提交
form 以form表单的形式提交 仅支持POST
dataType 参数的数据类型 只作为标志说明,并没有实际验证
Long
String
name 接收参数名
value 接收参数的意义描述
required 参数是否必填
true 必填
false 非必填
defaultValue 默认值

3-knife4j

3.1-pom.xml依赖

<dependency>
     <groupId>com.github.xiaoymin</groupId>
     <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>3.0.2</version>
</dependency>

3.2-配置类Swagger2Configuration.java

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class Swagger2Configuration {

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName("1.0")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("cn.ztuo.swagger"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API文档测试")
                .description("API文档")
                .version("1.0")
                .build();
    }
}

3.3-knife4j说明

以上有两个注解需要特别说明,如下表:

注解 说明
@EnableSwagger2 该注解是Springfox-swagger框架提供的使用Swagger注解,该注解必须加
@EnableKnife4j 该注解是knife4j提供的增强注解,Ui提供了例如动态参数、参数过滤、接口排序等增强功能,如果你想使用这些增强功能就必须加该注解,否则可以不用加

3.4-测试

测试类同Swagger,注解同Swagger

访问http://host:port/doc.html

上一篇:SpringBoot Swagger2 Response统一默认返回信息


下一篇:哈希碰撞和哈希冲突