Springfox是什么,有什么用?
Springfox基于Swagger,能更方便的集成到spring boot 中,Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。Swagger的目标是对REST API定义一个标准的和语言无关的接口,可让人和计算机无需访问源码、文档或网络流量监测就可以发现和理解服务的能力。当通过Swagger进行正确定义,用户可以理解远程服务并使用最少实现逻辑与远程服务进行交互。与为底层编程所实现的接口类似,Swagger消除了调用服务时可能会有的猜测。
Springfox官方文档:http://springfox.github.io/springfox/docs/snapshot/
Springfox的依赖
1 2 3 4 5 6 7 8 9 10 |
< 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的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public 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测试数据" , //作者 "这是我的技术博客站点" , //链接显示文字 "http://HdhCmsTestkailing.pub" //网站链接 ); return apiInfo; } } |
测试的Controll
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
@RestController @RequestMapping ( "/user" ) public class SwaggerDemoController { @RequestMapping (value = "/demo2" ,method = RequestMethod.POST) @ApiOperation (value= "测试接口1" , notes= "测试接口详细描述" ) @ResponseBody ModelMap 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文档的详细内容,更多关于Spring boot整合Springfox生成restful在线api的资料请关注其它相关文章!
原文链接:http://HdhCmsTestkailing.pub/article/index/arcid/100.html
查看更多关于Spring boot整合Springfox生成restful的在线api文档的详细内容...