zhazhahui

导航

swagger

swagger的使用

导入依赖

  		<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>

在Java中的使用

//step2 申明swagger的配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig
{
    @Bean
    public Docket bulidDocket()
    {
        //docket类型为DocumentationType.SWAGGER_2
        return new Docket(DocumentationType.SWAGGER_2)
                //初始化具体APIinfo所需要包含的属性。
                .apiInfo( initAPIinfo())
                //初始化选择器
                .select()
                //具体生成swagger文档的包路径.
                .apis(RequestHandlerSelectors.basePackage("sunjob.springbootswagger.controller"))
               //编译初始化docket对象
                .build();
    }
    public ApiInfo initAPIinfo()
    {
        return new ApiInfoBuilder()
               //初始化标题
               .title("HelloSwagger interface document")
               //初始化联系人信息
               .contact(new Contact("milo","www.abc.com","sunjob@qq.com"))
               //接口文档版本信息
               .version("v1.0")
               //描述信息
               .description("这是swagger 框架使用的接口文档 ")
               .build();
    }
}

swagger的注解

@Api:用在类上,说明该类的作用。

@ApiOperation:注解来给API增加方法说明。

@ApiImplicitParams : 用在方法上包含一组参数说明。

@ApiImplicitParam:用来注解来给方法入参增加说明。
参数:
   ·paramType:指定参数放在哪个地方
      ··header:请求参数放置于Request Header,使用@RequestHeader获取
      ··query:请求参数放置于请求地址,使用@RequestParam获取
      ··path:(用于restful接口)-->请求参数的获取:@PathVariable
      ··body:(不常用)
      ··form(不常用)
   ·name:参数名
   ·dataType:参数类型
   ·required:参数是否必须传(true | false)
   ·value:说明参数的意思
   ·defaultValue:参数的默认值

@ApiResponses:用于表示一组响应

@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
      ——code:数字,例如400
      ——message:信息,例如"请求参数异常!"
      ——response:抛出异常的类   

@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)

@ApiModelProperty:描述一个model的属性

posted on 2021-06-06 09:29  zhazhahui  阅读(43)  评论(0编辑  收藏  举报