SpringBoot集成swagger的实例代码

Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。这篇文章主要介绍了SpringBoot集成swagger,需要的朋友可以参考下

Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。本文简单介绍了在项目中集成swagger的方法和一些常见问题。如果想深入分析项目源码,了解更多内容,见参考资料。

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。

对于搬砖的同学来说,写接口容易,写接口文档很烦,接口变动,维护接口文档就更更更烦,所以经常能发现文档与程序不匹配。

等过一段时间就连开发者也蒙圈了

Swagger2快速方便的解决了以上问题。一个能与Spring MVC程序配合组织出强大RESTful API文档的新宠儿。

下面直接上代码

pom.xml

   4.0.0com.zhongxin.wealthwealthweb0.0.1-SNAPSHOTjarwealthwebDemo project for Spring Boot org.springframework.bootspring-boot-starter-parent1.5.9.RELEASE UTF-8UTF-81.8  org.springframework.bootspring-boot-starter-web org.springframework.bootspring-boot-starter-testtest io.springfoxspringfox-swagger22.7.0 io.springfoxspringfox-swagger-ui2.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的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对html中文网网站的支持!

以上就是SpringBoot集成swagger的实例代码的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java