@ApiIgnore():用于類或者方法上落剪,可以不被swagger顯示在頁面上
@Api():用于類表示標(biāo)識這個類是swagger的資源
tags–表示說明 但是tags如果有多個值,會生成多個list
value–也是說明妈嘹,可以使用tags替代 (實(shí)際沒有作用)
@ApiOperation():用于方法惧辈;表示一個http請求的操作
value用于方法描述
notes用于提示內(nèi)容
tags可以重新分組(視情況而用)
@ApiImplicitParams():用于方法,包含多個 @ApiImplicitParam
name–參數(shù)ming
value–參數(shù)說明
required 是否必須 boolean
dataType–數(shù)據(jù)類型
paramType–參數(shù)類型
body 使用@RequestBody接收數(shù)據(jù) POST有效
path 在url中配置{}的參數(shù)
query 普通查詢參數(shù) 例如 ?query=q ,jquery ajax中data設(shè)置的值也可以,例如 {query:”q”},springMVC中不需要添加注解接收
header 使用@RequestHeader接收數(shù)據(jù)
form 筆者未使用,請查看官方API文檔
example–舉例說明
@ApiImplicitParam(): 用于方法 表示單獨(dú)的請求參數(shù)
表示單獨(dú)的請求參數(shù)
@ApiResponses():用于表示一組響應(yīng)
@ApiResponse():用在@ApiResponses中薄腻,一般用于表達(dá)一個錯誤的響應(yīng)信息
code:數(shù)字,例如400
message:信息典鸡,例如"請求參數(shù)沒填好"
response:拋出異常的類
@ApiParam():用于方法被廓,參數(shù),字段說明萝玷;表示對參數(shù)的添加元數(shù)據(jù)(說明或是否必填等)
name–參數(shù)名
value–參數(shù)說明
required–是否必填
@ApiModel():用于類 表示對類進(jìn)行說明,用于參數(shù)用實(shí)體類接收
value–表示對象名
description–描述
都可省略
@ApiModelProperty():用于方法昆婿,字段 表示對model屬性的說明或者數(shù)據(jù)操作更改
value–字段說明
required–是否必填
name–重寫屬性名字
dataType–重寫屬性類型
example–舉例說明
hidden–隱藏
- demo
import com.max256.morpho.sys.entity.TestEntity;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
@Api(tags = "表示說明") // 標(biāo)識這個類是swagger的資源
@RestController
@RequestMapping("/test")
public class TestController {
@ApiOperation(value = "方法描述",notes = "提示內(nèi)容",httpMethod = "get") // 用于方法球碉;表示一個http請求的操作
@ApiImplicitParams({
@ApiImplicitParam(name = "s",value = "參數(shù)說明",required = true,dataType = "string",example = "start"),
@ApiImplicitParam(name = "n",value = "參數(shù)說明",required = false,dataType = "string",example = "end")
})
@ApiResponses({
@ApiResponse(code = 200,message = "success_",response = String.class),
@ApiResponse(code = 500,message = "error_",response = String.class),
})
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test(
String s,String n
// 此處使用@ApiParam Swagger解析會出問題,但不影響請求
// @ApiParam(name = "s",value = "s參數(shù)",required = true,example = "start") String s,
// @ApiParam(name = "n",value = "n參數(shù)",required = false,example = "start") String n
){
return s + " - test - " + n;
}
@ApiOperation(value = "方法描述",notes = "提示內(nèi)容",httpMethod = "post")
@RequestMapping(value = "/testEntity",method = RequestMethod.POST)
public TestEntity testEntity(@RequestBody TestEntity testEntity){
return testEntity;
}
@RequestMapping(value = "/testPathVarible/{param}",method = RequestMethod.GET)
public String TestPathVarible(@ApiParam(name = "param",value = "參數(shù)說明",required = true)@PathVariable(name = "param") String params){
return params;
}
}
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@ApiModel
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class TestEntity {
@ApiModelProperty(name = "name",value = "姓名",example = "lb")
private String name;
@ApiModelProperty(hidden = true)
private String value;
@ApiModelProperty(name = "age",value = "年齡",example = "16")
private int age;
}