天天看点

Swagger-UI的使用一、添加依赖二、创建配置类三、注解

一、添加依赖

        在pom.xml文件中添加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>
           

二、创建配置类

        swagger的configuration

@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfiguration {
    //配置swagger的Docket的bean的实例
    @Bean
    public Docket createRestApi() {

        //public Docket createRestApi(Environment environment)
        //设置要显示的Swagger环境Profiles profiles = Profiles.of("dev", "pro");
        //Profiles profiles = Profiles.of("dev");
        //通过environment.acceptsProfiles判断是否处在自己设定的环境当中
        //boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //enable是否启动Swagger,如果为false,则Swagger不能在浏览器中访问
                //.enable(flag)
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式
                //basePackage指定要扫描的包
                //any() 扫描全部
                .apis(RequestHandlerSelectors.basePackage("com.until.oracle.controller"))
                //过滤什么路径
                .paths(PathSelectors.any())
                .build();
    }

    //配置Swagger2信息
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger构建api文档")//网站标题
                .description("简单优雅的restfun风格,http://blog.csdn.net/saytime")//网站描述
                .version("v1.0")//版本号
                .contact(new Contact("吴", "http://blog.csdn.net/saytime", "[email protected]"))//联系人信息
                .termsOfServiceUrl("http://blog.csdn.net/saytime")
                .license("Apache 2.0")//协议
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
                .build();
    }
}
           

三、注解

@ApiIgnore 可以用在类、方法上,方法参数中,用来屏蔽某些接口或参数,使其不在swagger-UI页面上显示。

@Api

用在类上,说明该类的作用。可以标记一个Controller类做为swagger 文档资源,使用方式

@Api(value = "用户", tags = {"用户信息模块"})
           

@ApiOperation用在方法上,说明方法的作用,每一个url资源的定义,使用方式

@ApiOperation(value="查询所有用户信息", notes="获取所有用户信息")
           
Swagger-UI的使用一、添加依赖二、创建配置类三、注解

@ApiImplicitParams用于方法,包含多个 @ApiImplicitParam

1、name :参数名 

2、value : 描述参数名 

3、required : 该参数是否必填

4、dataType :参数的数据类型 

5、example:举例 

6、paramType :查询参数类型。这里有几种形式:

类型 作用
path 以地址的形式提交数据
query 直接跟参数完成自动映射赋值
body 以流的形式提交 仅支持POST
header  参数在request headers 里边提交
form 以form表单的形式提交 仅支持POST
@ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "query")
    })
           

继续阅读