springboot 集成 swagger 自动生成API文档
Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。简单来说,Swagger是一个功能强大的接口管理工具,并且提供了多种编程语言的前后端分离解决方案。
- Swagger可以整合到代码中,在开发时通过注解,编写注释,自动生成API文档;
- 方便前后端分离开发
swagger是什么?
API Developmentfor Everyone
springboot中集成swagger
1、添加pom依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
2、添加swagger配置
@EnableSwagger2 @RestController @SpringBootApplication public class FamilyServerApplication { public static void main(String[] args) { SpringApplication.run(FamilyServerApplication.class, args); } @RequestMapping("/") public String hello(){ return "hello billy!"; } }
3、在需要暴露的API上添加API注解
例如:
@Api("MemberManageController") @RestController @RequestMapping("member") public class MemberManageController extends BaseController { @Autowired private MembersManageService manageService; /** * 获取成员 * @return */ @ApiOperation(value = "查询家庭成员", notes = "查询家庭成员") @ApiImplicitParams(@ApiImplicitParam(name = "name", value = "姓名", paramType = "query", dataType = "String")) @RequestMapping("getMembers") public Object getMembers(String name){ logger.debug("开始。。。"); System.out.println(name); Page<FamilyMembers> page = new Page<>(); IPage<FamilyMembers> membersByPage = manageService.getMembersByPage(page); Messager result = messager.successObjectResponse(membersByPage); return result; } }
4、启动项目,访问swagger-ui地址
演示项目地址为: http://localhost:9090/swagger-ui.html
结果如下:
5、接口调试
返回结果:
备注:项目开发中,如果前后端分离,采用swagger特别方便。
让每一天过的有意义!