「这是我参与11月更文挑战的第27天,活动详情查看:2021最后一次更文挑战」
相关文章
Java随笔记:Java随笔记
前言
- 突然发现自己漏东西了,文档描述的注解好像只讲了两个。。
- 在此补上,方便大家还有我自己回头的查看。
一、Api
@Api
标注在类上,主要是说明类的作用。1
2
3
4
5
6
7
8
9less复制代码@RestController
@Api(tags = "Swagger测试类")
public class SwaggerTestController {
@RequestMapping(value = "test-swagger",method = RequestMethod.GET)
public StudentResponse dyTest(){
return new StudentResponse();
}
}重启后效果如下:
二、ApiModel
@ApiModel
用在类上,表示对类进行说明,用于实体类中的参数接收说明1
2
3
4
5
6
7
8
9
10
11less复制代码@Data
@ApiModel(value = "StudentResponse",description = "测试类返回结果")
public class StudentResponse{
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "年龄")
private int age;
@ApiModelProperty(value = "爱好")
private String like;
}效果如下:
三、ApiModelProperty
@ApiModelProperty()
用于字段,表示对 model 属性的说明。1
2
3
4
5
6
7
8
9
10
11less复制代码@Data
@ApiModel(value = "学生类",description = "这是类的详细描述信息呀")
public class StudentResponse{
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "年龄")
private int age;
@ApiModelProperty(value = "爱好")
private String like;
}效果如下:
四、ApiParam
@ApiParam
用于 Controller 中方法的参数说明。新建添加实体类
1
2
3
4
5
6
7
8
9
10
11
12less复制代码@Data
@ApiModel(value = "StudentRequest",description = "添加学生实体类")
public class StudentRequest {
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "年龄")
private int age;
@ApiModelProperty(value = "爱好")
private String like;
}
1
2
3
4
5
6
7
8
9
10less复制代码@RestController
@Api(tags = "Swagger测试类")
public class SwaggerTestController {
@PostMapping("Student")
@ApiResponses({@ApiResponse(code = 200,message = "success", response = StudentResponse.class),
@ApiResponse(code = 500,message = "failed", response = StudentResponse.class)})
@ApiOperation(value = "新增学生信息")
public void addStudent(@ApiParam(value = "学生类", required = true) StudentRequest studentRequest){
}
}效果如下:
五、ApiOperation
@ApiOperation
用在 Controller 里的方法上,说明方法的作用。
1
2
3
4
5
6
7
8
9less复制代码@RestController
@Api(tags = "Swagger测试类")
public class SwaggerTestController {
@RequestMapping(value = "test-swagger",method = RequestMethod.GET)
@ApiOperation(value = "查询学生详情",notes = "参数为id")
public StudentResponse dyTest(){
return new StudentResponse();
}
}效果如下:
总结
- 这几个注解,也是我们平时使用最多的注解。
- 莫急,未完待续,明儿我们再讲解几个常用注解。
- 本篇文章没有像之前的讲的那么细,我认为只要将方法教了,具体的学习方式每个人都是不同的。
- 感兴趣的小伙伴们可以自己点进去看看都有哪些参数,可以自己每个都试试,玩玩!
- 希望对你们有所帮助!谢谢!
路漫漫其修远兮,吾必将上下求索~
如果你认为i博主写的不错!写作不易,请点赞、关注、评论给博主一个鼓励吧~hahah
本文转载自: 掘金