swagger用于定义API文档,好处:
前后端分离开发API文档非常明确测试的时候不需要再使用URL输入浏览器的方式来访问Controller传统的输入URL的测试方式对于post请求的传参比较麻烦(当然,可以使用postman这样的浏览器插件)sPRingfox基于swagger2,兼容老版本利用Springfox集成Swagger主要分为两步:<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version></dependency>然后创建Swagger配置类SwaggerConfig此时便可通过访问/api-docs可以看到json数据。2. Springmvc4集成Swagger-ui
首先添加Maven依赖:<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version></dependency>由于springfox-swagger-ui采用webjar的方式引入页面,需要在sping xml中配置静态资源访问:<!-- Enables swgger ui--> <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/> <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>集成swagger UI之后访问http://localhost:8080/swagger-ui.html 即可看到swagger界面。注意:swagger UI基于/api-docs中提供的数据,若设置springfox时没有在/api-docs中添加相关json数据,swagger UI编译验证过程会报错。验证需要引入commons-lang3包,没有的需要添加依赖。<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version></dependency>
新闻热点
疑难解答