@Api
(tags =
"Swagger测试类"
)
public class SwaggerTestController {
@ApiResponse
(code =
200
,message =
"success"
, response = StudentResponse.class)
@RequestMapping
(value =
"test-swagger"
,method = RequestMethod.GET)
@ApiOperation
(value =
"查询学生详情"
,notes =
"参数为id"
)
public StudentResponse
dyTest
(){
return
new
StudentResponse
();
效果如下:
二、ApiResponses
上面的只定义了
200
的状态码,如果是
500
呢?
@ApiResponses
就是多个
@ApiResponse
组装起来。
@RestController
@Api(tags = "Swagger测试类")
public class SwaggerTestController {
@ApiResponses({@ApiResponse(code = 200,message = "success", response = StudentResponse.class),
@ApiResponse(code = 500,message = "failed", response = StudentResponse.class)})
@RequestMapping(value = "test-swagger",method = RequestMethod.GET)
@ApiOperation(value = "查询学生详情",notes = "参数为id")
public StudentResponse dyTest(){
return new StudentResponse();
效果如下:
三、ApiImplicitParam
如果我们的参数较少的时候,比如,根据id查询指定数据详细信息的时候,我们总不能单独建个实体来描述吧?
通常我们的做法时,五个参数以下时,可以通过ApiImplicitParam
来对参数进行描述。
当然,只限于Get
请求,如果是添加、修改等操作,Post
、Put
请求时还是以实体类为主!
@ApiImplicitParam
用于方法上,为单独的请求参数进行说明。
@RestController
@Api(tags = "Swagger测试类")
public class SwaggerTestController {
@ApiResponses({@ApiResponse(code = 200,message = "success", response = StudentResponse.class),
@ApiResponse(code = 500,message = "failed", response = StudentResponse.class)})
@RequestMapping(value = "test-swagger",method = RequestMethod.GET)
@ApiOperation(value = "查询学生详情",notes = "参数为id")
@ApiImplicitParam(name = "id", value = "学生ID", dataType = "string", paramType = "query", required = true, defaultValue = "1")
public StudentResponse dyTest(String id){
return new StudentResponse();
效果如下:
四、ApiImplicitParams
多个参数时,ApiImplicitParam
可能就满足不了我们的要求了,这时候需要组装下!
同样的,多个@ApiImplicitParam
组装起来。
@RestController
@Api(tags = "Swagger测试类")
public class SwaggerTestController {
@ApiResponses({@ApiResponse(code = 200,message = "success", response = StudentResponse.class),
@ApiResponse(code = 500,message = "failed", response = StudentResponse.class)})
@RequestMapping(value = "test-swagger",method = RequestMethod.GET)
@ApiOperation(value = "查询学生详情",notes = "参数为id")
@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "学生ID", dataType = "string", paramType = "query", required = true, defaultValue = "1"),
@ApiImplicitParam(name = "name", value = "学生姓名", dataType = "string", paramType = "query", required = true, defaultValue = "1")})
public StudentResponse dyTest(String id){
return new StudentResponse();
效果如下:
到这里Swagger
系列是真的结束了。再也没有东西可讲了。
看完本系列的所有文章,相信你对Swagger
有了相对来说比较深得了解了。
希望对你有所帮助,本系列不说是全网最详细也差不多啦吧!
哈哈哈,我可以整理了很多的才最终整合出来的!
本系列文章都是个人见解,如有不对,敬请指出~
轻喷!谢谢~
路漫漫其修远兮,吾必将上下求索~
如果你认为i博主写的不错!写作不易,请点赞、关注、评论给博主一个鼓励吧~hahah