SwaggerUI日常使用

  最近公司项目集成springFox,记录一些swaggerUI日常使用,包括数组,文件,默认值,允许值,参数/结果类注解,响应码..等等。

   一.参数注解:

  单参数:@ApiImplicitParam

  多参数:@ApiImplicitParams

1 @ApiImplicitParams({
2             @ApiImplicitParam(name = "pageIndex",value = "页码,默认值:0",dataType = "int",
3                     paramType = "query"),
4             @ApiImplicitParam(name = "pageSize",value = "页长,默认值:20",dataType = "int",
5                     paramType = "query")
6     })

  可以看一下参数注解接口相关属性如下:

 1 public @interface ApiImplicitParam {
 2     //参数名
 3     String name() default "";
 4     //参数作用,对应中文名
 5     String value() default "";
 6     //默认值
 7     String defaultValue() default "";
 8     //允许值的范围,逗号分隔
 9     String allowableValues() default "";
10     //是否必传,可以看到默认是非必填的
11     boolean required() default false;
12     //暂时没用到
13     String access() default "";
14     //是否允许数组
15     boolean allowMultiple() default false;
16     //参数类型:int/String之类的,注:不支持integer
17     String dataType() default "";
18     //参数作用类型:比如query正常查询参数,path表示路径参数
19     String paramType() default "";
20     //举例
21     String example() default "";
22 
23     Example examples() default @Example({@ExampleProperty(
24     mediaType = "",
25     value = ""
26 )}); 

  下面详细介绍下几种应用:

  1.文件类型:

@ApiImplicitParam(name = "file",value = "文件",dataType = "file",paramType = "query")

  2.数组,以string数组为例

@ApiImplicitParam(name = "type",value = "类型",dataType = "String",  paramType = "query",allowMultiple = true),

  3.默认值

 @ApiImplicitParam(name = "type",value = "类型",dataType = "String",paramType = "query",defaultValue = "test"),

  4.允许值,比如状态,开关类字段,一般只支持0/1

@ApiImplicitParam(name = "status",value = "状态",dataType = "int",paramType = "query",allowableValues = "0,1"),

       5.参数/结果类注解 一般常用@requestbody封装的参数类,或者设定的结果类。

public void create(@ApiParam @RequestBody Entity entity)

  然后在对应的参数类中添加响应的@apimodel和@apiproperty注解   

@ApiModel
public class Entity {
    @ApiModelProperty(name = "id",value = "主键")
    private int id;                                     

    @ApiModelProperty(name = "type",value = "类型",)
    private String type;                                
}

  6.响应码

@ApiResponses({
            @ApiResponse(code = 200,message = "操作成功"),
                    
    })

  在swaggerui界面的model中可以看到生成效果,这里我只加了前两个注解  

  7.必填项,默认非必填。注:必填项一定要和接口设定一致。 

@ApiImplicitParam(name = "id",value = "id",dataType = "String", paramType = "query",required = true)

  8.路径参数,比如接口路径为 /type/{id},可以如下定义注解 

@ApiImplicitParam(name = "id",value = "id",dataType = "String", paramType = "path",required = true)

 

 

待补充...

 

posted @ 2018-08-17 15:12  轻天  阅读(5530)  评论(0编辑  收藏  举报