小编给大家分享一下SpringBoot如何集成swagger,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
专注于为中小企业提供成都做网站、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业连城免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。本文简单介绍了在项目中集成swagger的方法和一些常见问题。如果想深入分析项目源码,了解更多内容,见参考资料。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。
对于搬砖的同学来说,写接口容易,写接口文档很烦,接口变动,维护接口文档就更更更烦,所以经常能发现文档与程序不匹配。
等过一段时间就连开发者也蒙圈了
Swagger2快速方便的解决了以上问题。一个能与Spring MVC程序配合组织出强大RESTful API文档的新宠儿。
下面直接上代码
pom.xml
4.0.0 com.zhongxin.wealth wealthweb 0.0.1-SNAPSHOT jar wealthweb Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test io.springfox springfox-swagger2 2.7.0 io.springfox springfox-swagger-ui 2.7.0
创建配置类
package com.zhongxin.wealth.apiConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Created by DingYS on 2017/12/8.
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zhongxin.wealth.web"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("廊坊委贷大数据统计结果输出接口")
.version("1.0")
.build();
}
}controller编写
package com.zhongxin.wealth.web;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by DingYS on 2017/12/7.
*/
@RestController
@RequestMapping("/hello")
public class HelloWordController {
@ApiOperation(value="测试接口", notes="这只是一个测试controller调用的接口,没有任何的业务逻辑")
@RequestMapping(value = {"/test"},method = RequestMethod.GET)
public String testHello(){
return "hello";
}
}代码完成,准备看效果

点击Try it out!

是不是很详细,很高大上。
注:集成过程中刚开始用的swagger2.2.2版本,会在首页出现一个error的错误提醒
{“schemaValidationMessages”:[{“level”:”error”,”message”:”Can't read from file http://127.0.0.1:8888/v2/api-docs"}]}但是浏览器访问:http://127.0.0.1:8888/v2/api-docs 又能获取 结果
{“swagger”:”2.0”,”info”:{“version”:”1.0”,”title”:”廊坊委贷大数据统计结果输出接口”,”contact”:{},”license”:{}},”host”:”127.0.0.1:8888”,”basePath”:”/“,”tags”:[{“name”:”hello-word-controller”,”description”:”Hello Word Controller”}],”paths”:{“/hello/test”:{“get”:{“tags”:[“hello-word-controller”],”summary”:”测试接口”,”description”:”这只是一个测试controller调用的接口,没有任何的业务逻辑”,”operationId”:”testHelloUsingGET”,”consumes”:[“application/json”],”produces”:[“/“],”responses”:{“200”:{“description”:”OK”,”schema”:{“type”:”string”}},”401”:{“description”:”Unauthorized”},”403”:{“description”:”Forbidden”},”404”:{“description”:”Not Found”}}}}}}具体原因本人不明,换成2.7.0版本以后没在出现。
看完了这篇文章,相信你对“SpringBoot如何集成swagger”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
本文标题:SpringBoot如何集成swagger
URL分享:http://www.jxjierui.cn/article/gojpop.html


咨询
建站咨询
