小编给大家分享一下SpringBoot如何集成swagger,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。本文简单介绍了在项目中集成swagger的方法和一些常见问题。如果想深入分析项目源码,了解更多内容,见参考资料。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。
对于搬砖的同学来说,写接口容易,写接口文档很烦,接口变动,维护接口文档就更更更烦,所以经常能发现文档与程序不匹配。
等过一段时间就连开发者也蒙圈了
Swagger2快速方便的解决了以上问题。一个能与Spring MVC程序配合组织出强大RESTful API文档的新宠儿。
下面直接上代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhongxin.wealth</groupId>
<artifactId>wealthweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>wealthweb</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
</project>
创建配置类
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”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。