2019-06-27 springboot 中使用swagger

2019-06-27  本文已影响0人  zz云飞扬

必须步骤:1、导入相应jar包, 2、建立config 类 。完成。

1/ 添加依赖

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

2、配置config

@Configuration

@EnableSwagger2

public class SwaggerConfig {

@Bean

    public Docket createRestApi(){

//扫包配置,路径规则配置

        return new              Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandler       Selectors.basePackage("com.zbiti.swaggertest.api")).paths(PathSelectors.any()).build();

}

//页面显示标题,描述配置

    private ApiInfo apiInfo(){

return new ApiInfoBuilder().title("zzTestSwagger").description("learn how to start swagger").termsOfServiceUrl("").build();

}

}

配置config

3、使用 注解@api ,@ApiOperation() ,@ApiImplicitParam

@Api("SwaggerTestApi")

@RestController

public class SwaggerApi {

@ApiOperation("测试用")

@ApiImplicitParams({@ApiImplicitParam(name="userName",value="用户名",required =true),@ApiImplicitParam(name ="age",dataType ="int")})

@GetMapping("/indexApi")

public String indexApi(String userName,int age){

return "username:"+userName+"---"+"age:"+age;

}

}

swagger 注解使用

 yml 中不需要特殊配置

application.yml

 访问 http://localhost:1011/swagger-ui.html

进入swagger-ui swagger-ui详情

@ApiImplicitParam, 网上找到部分参数说明, dataType 我测试时写了一个 int 也是ok的。

属性 取值 作用

paramType   查询参数类型

      path 以地址的形式提交参数数据

      query 直接跟参数完成自动映射赋值

      body 以流的形式提交 仅支持POST

      header 参数在request headers 里边提交

      form 以form表单的形式提交 仅支持POST

dataType   参数的数据类型 只作为标志说明,并没有实际验证

      Long  

      String  

name   接收参数名

value   接收参数的意义描述

required   参数是否必填

      true 必填

      false 非必填

defaultValue   默认值

上一篇 下一篇

猜你喜欢

热点阅读