前后端分离开发模式中,api文档是最好的沟通方式。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
1.及时性 (接口变更后,能够及时准确哪拿纯地通知相关前后端开发人员)
2.规范性 (并且保证接口的规范性,如接口的地址,请求方式,参数及响应格式和错误信息)
3.一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)
4.可测性 (直接在接口文档上进行测试,以方便理解业务)
在日常写接口过程中,有些接口是无法直接在浏览器中进行测试,这时候我们就可以采用接口测试工具。
在我所了解的接口测试工具中,常用的应该就是postman和Swagger了。但是对于后端开发人员来说,更倾向于使用Swagger。
以我的多Model项目为例
在顶级父工程下创建common模块,并在其pom.xml文件中引入Swagger依赖李咐
引入依赖后在common模块下再创建common_base模块并创建Swagger配置类
Swagger配置类:
这样Swagger大致就配置好了,接下来,就往接口模块进行引入。
在模块service模块引入service_base
在service的子模块——service_edu模块的启动类中添加 @ComponentScan 注解
这样,我们便可以对功能接口通过Swagger进行测试了。
我们也可以进行一些自定义设置
1.启动你的项目
2.访问路径 http://localhost :你的端口号/swagger-ui.html
3.进行测试
这里我用查询讲师列表为例
以上就是使用Swagger进行接口测试整合方法敏含的简单介绍。
为什么要引入Swagger?
有过后台开发和前端联调的人都会被接口文档折磨,更新不及时,文档和代码不一致,无法调试,swagger就是为了解决这个问题。
看下swagger官方介绍
Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保兄槐持同步。
如此好的利器,怎么在后台框架中快速集成呢?
第一步、添加祥尘笑maven依赖
需要在系统的pom中添加如下依赖:
第二步、添加swagger配置文件
第三步、测试
浏览器输入:http://localhost:8080/swagger-ui.html ,能测试生成的api是否可用。谨含
总结
很好用的开源框架,集成也很简单,建议大家在工程中使用,能够快速开发,减少前后端沟通api的时间成本。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务,它有着如下的优点:
1)及时性 (接口变更后,能够及时准确地通知相关前后端开发人员)
2)规范性 (并且保证接口的规范性,如接口的地址,请求渗行方式,参数及响应格式和错误信息)
3)一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)
4)可测性 (直接在接口文档上进行测试,以方便理解业务)
1)添加pom.xml依赖
<pre class="md-fences md-end-block md-fences-with-lineno" lang="xml" contenteditable="false" cid="n252" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial"启姿>
<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></pre>
2)创建swagger配置文件
<pre class="md-fences md-end-block md-fences-with-lineno" lang="java"丛旁哗 contenteditable="false" cid="n255" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
@Configuration
@EnableSwagger2
public class Swagger2Config {
}</pre>
3)启动程序
访问路径: http://localhost:8081/swagger-ui.html ,出现生成的文档页面。
此为原生界面,很难看,所以我们需要引入swagger-bootstrap-ui,也就是Knife4j的前身
4)使用Knife4j
a)添加pom.xml依赖
<pre class="md-fences md-end-block md-fences-with-lineno" lang="xml" contenteditable="false" cid="n268" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency></pre>
b)开启Swagger2配置
<pre class="md-fences md-end-block md-fences-with-lineno" lang="java" contenteditable="false" cid="n271" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
import java.util.ArrayList
import java.util.List
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.apiInfo(apiInfo())
.select()
//swagger要扫描的包路径
.apis(RequestHandlerSelectors.basePackage("com.dsblog.server.controller"))
.paths(PathSelectors.any())
.build()
}
b)重启服务
访问地址: http://localhost:8081/doc.html ,这个ui界面看起来就更美观,更符合国人的使用习惯
1、添加pom.xml依赖
<pre class="md-fences md-end-block md-fences-with-lineno" lang="xml" contenteditable="false" cid="n282" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
<?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>
</project></pre>
2、创建Swagger2Config配置文件
<pre class="md-fences md-end-block md-fences-with-lineno" lang="java" contenteditable="false" cid="n285" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
package com.dsblog.server.config
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.*
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spi.service.contexts.SecurityContext
import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.swagger2.annotations.EnableSwagger2
import java.util.ArrayList
import java.util.List
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.apiInfo(apiInfo())
.select()
//swagger要扫描的包路径
.apis(RequestHandlerSelectors.basePackage("com.dsblog.server.controller"))
.paths(PathSelectors.any())
.build()
.securityContexts(securityContexts())
.securitySchemes(securitySchemes())
}
}</pre>
3、创建SecurityConfig配置文件
<pre class="md-fences md-end-block md-fences-with-lineno" lang="java" contenteditable="false" cid="n288" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
package com.dsblog.server.config.security
import com.dsblog.server.model.User
import com.dsblog.server.service.IUserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.builders.WebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}</pre>
4、创建测试Controller,其中/login登录和/logout退出方法不需要Authorize验证,和上面的重写方法<mark style="box-sizing: border-boxbackground: rgb(255, 255, 0)color: rgb(0, 0, 0)">configure(WebSecurity web)</mark>匹配,user/info方法需要Authorize验证才能进行访问
<pre class="md-fences md-end-block md-fences-with-lineno" lang="java" contenteditable="false" cid="n291" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
package com.dsblog.server.controller
import com.dsblog.server.model.ResultBean
import com.dsblog.server.model.User
import com.dsblog.server.model.UserLoginParam
import com.dsblog.server.service.IUserService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest
import java.security.Principal
@Api(tags = "LoginController")
@RestController
public class LoginController {
}</pre>
5、重启程序,输入 http://localhost:8081/doc.html
6、测试
5)携带token发送到后台获取用户信息,验证通过
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)