Spring boot整合Springfox在线生成restful的api doc

来源:互联网 发布:支付宝贷款软件 编辑:程序博客网 时间:2024/06/13 11:03
小陈博主 2017-10-24 22:12

Springfox是什么,有什么用?

Springfox基于Swagger,能更方便的集成到spring boot 中,Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。Swagger的目标是对REST API定义一个标准的和语言无关的接口,可让人和计算机无需访问源码、文档或网络流量监测就可以发现和理解服务的能力。当通过Swagger进行正确定义,用户可以理解远程服务并使用最少实现逻辑与远程服务进行交互。与为底层编程所实现的接口类似,Swagger消除了调用服务时可能会有的猜测。

Springfox官方文档:http://springfox.github.io/springfox/docs/snapshot/

我的整合项目地址:https://coding.net/u/kailingchen/p/SwaggerDemo/git

Springfox的依赖

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.4.0</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.4.0</version>

</dependency>

Springfox的配置

@Configuration@EnableSwagger2public class SwaggerConfig {@Beanpublic Docket demoApi() {return new Docket(DocumentationType.SWAGGER_2).groupName("demo").genericModelSubstitutes(DeferredResult.class).useDefaultResponseMessages(false).forCodeGeneration(false)//.pathMapping("/") //根路径.select().paths(PathSelectors.regex("/user/.*"))//筛选展示的接口,使用PathSelectors.any(),展示所有接口.build().apiInfo(demoApiInfo());}//api信息private ApiInfo demoApiInfo() {ApiInfo apiInfo = new ApiInfo("自己平台的api",//大标题"swagger搭建api平台",//小标题"1.0",//版本"NO terms of service","632104866@qq.com",//作者"这是我的技术博客站点",//链接显示文字"http://www.kailing.pub"//网站链接);return apiInfo;}}

测试的Controll

@RestController@RequestMapping("/user")public class SwaggerDemoController {@RequestMapping(value = "/demo2",method = RequestMethod.POST)@ApiOperation(value="测试接口1", notes="测试接口详细描述")@ResponseBodyModelMap getDemo(@RequestBody User user) {ModelMap map = new ModelMap();map.addAttribute("userId", 111);map.addAttribute("userName", "kl博客");return map;}@ResponseBody@RequestMapping(value = "/demo3", method = RequestMethod.POST)@ApiOperation(value="测试接口2", notes="测试接口详细描述",code = 200,produces = "application/json")public ModelMap getDemoa(@RequestParam("name") String demoName, @RequestParam String content) {ModelMap map = new ModelMap();map.addAttribute("result",demoName + "AAA");return map;}@ResponseBody@ApiIgnore //使用这个注解忽略这个接口@RequestMapping(value = "/demo4", method = RequestMethod.POST)public ModelMap getDemob(@RequestParam String content) {ModelMap map = new ModelMap();map.addAttribute("result",new java.util.Date());return map;}}
在浏览器输入http://localhost:8080/swagger-ui.html,可查看是否整合成功

至此我们的额api在线文档整合完成了,下面是效果图

Spring boot整合Springfox在线生成restful的api doc

阅读全文
0 0
原创粉丝点击