官方文档
https://swagger.io/tools/swagger-ui/
Swagger UI allows anyone — be it your development team or your end consumers — to visualize and interact with the API’s resources without having any of the implementation logic in place. It’s automatically generated from your OpenAPI (formerly known as Swagger) Specification, with the visual documentation making it easy for back end implementation and client side consumption. -官网说明
用来管理api的一款开发工具,目的在于更规范,更快速,更方便的设计,管理,编写RESTful api以及api文档。不局限于某项语言
简单来说就是自动帮我写接口的文档的工具。
先快速上手使用,后面再介绍具体的细节
新建springboot项目
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>2.9.2version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>2.9.2version>
dependency>
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
常用注解:
@Api: 修饰整个类,用于controller类上
@ApiOperation: 描述一个接口,用户controller方法上
@ApiParam: 单个参数描述
@ApiModel: 用来对象接收参数,即返回对象
@ApiModelProperty: 对象接收参数时,描述对象的字段
@ApiResponse: Http响应其中的描述,在ApiResonse中
@ApiResponses: Http响应所有的描述,用在
@ApiIgnore: 忽略这个API
@ApiError: 发生错误的返回信息
@ApiImplicitParam: 一个请求参数
看代码:
@Api(tags = "查询课程的接口")
@Controller
@RequestMapping("/lesson")
public class LessonController {
final
TeacherService teacherService;
private final LessonService lessonService;
public LessonController(LessonService lessonService, TeacherService teacherService) {
this.lessonService = lessonService;
this.teacherService = teacherService;
}
//完成 按课程名
@ApiOperation("按课程名查询")
@ApiImplicitParam(name = "name", value = "课程名字", defaultValue = "99", required = true)
@GetMapping("/lessonName/{name}")
public String getLessonByLessonName(@PathVariable("name") String name, Model model){
List<Lesson> lessons = lessonService.selectByLessonName(name);
for (Lesson lesson : lessons) {
lesson.setTeacherName(teacherService.selectById(lesson.getTeacherId()).getName());
}
model.addAttribute("lessons",lessons);
return "home";
}
//完成 按老师名
@ApiOperation("按老师姓名查询")
@GetMapping("/teacherName/{teacherName}")
public String getLessonByTeacherId(@PathVariable("teacherName") String teacherName,Model model){
Teacher teacher = teacherService.selectByName(teacherName);
List<Lesson> lessons = lessonService.selectByTeacherID(teacher.getId());
for (Lesson lesson : lessons) {
lesson.setTeacherName(teacherName);
System.out.println(lesson);
}
model.addAttribute("lessons",lessons);
return "home";
}
//完成 按上课地点
@ApiOperation("按上课地点查询")
@GetMapping("/address/{address}")
public String getLessonByAddress(@PathVariable("address") String address,Model model){
List<Lesson> lessons = lessonService.selectByAddress(address);
for (Lesson lesson : lessons) {
lesson.setTeacherName(teacherService.selectById(lesson.getTeacherId()).getName());
System.out.println(lesson);
}
model.addAttribute("lessons",lessons);
return "home";
}
//完成,获取今日课程
@ApiOperation("今日课表查询")
@GetMapping("/date/{date}")
public String getLessonByDate(@PathVariable("date") String date,Model model){
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
// System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
List<Lesson> lessons = lessonService.selectByDate(date);
for (Lesson lesson : lessons) {
lesson.setTeacherName(teacherService.selectById(lesson.getTeacherId()).getName());
System.out.println(lesson);
}
model.addAttribute("lessons",lessons);
return "home";
}
@ApiOperation("按时间区间查询")
@GetMapping("/date/{dateBegin}/{dateEnd}")
public String getRangeLesson(@PathVariable String dateBegin, @PathVariable String dateEnd,Model model){
List<Lesson> lessons = lessonService.selectRangeDate(dateBegin,dateEnd);
for (Lesson lesson : lessons) {
lesson.setTeacherName(teacherService.selectById(lesson.getTeacherId()).getName());
System.out.println(lesson);
}
model.addAttribute("lessons",lessons);
return "home";
}
}
运行后在http://localhost:8080/swagger-ui.html,即可查看文档
终于不用苦逼的写接口文档了qaq
在里面还有一些其他很多功能,就需要自己去了解了
参考博客
https://www.jianshu.com/p/349e130e40d5
https://www.jianshu.com/p/4fdac2a10c79
https://www.cnblogs.com/xifengxiaoma/p/11022146.html
https://blog.csdn.net/u012702547/article/details/88775298