国外电商网站如何建立,温州专业营销网站制作,河南建设信息网站,深圳网站设计专家乐云seo品牌我的飞书:https://rvg7rs2jk1g.feishu.cn/docx/TVlJdMgYLoDJrsxAwMgcCE14nxt 使用Springfox Swagger生成API#xff0c;并导入Postman#xff0c;完成API单元测试
Swagger: 是一套API定义的规范#xff0c;按照这套规范的要求去定义接口及接口相关信息#xff0c;再通过可…我的飞书:https://rvg7rs2jk1g.feishu.cn/docx/TVlJdMgYLoDJrsxAwMgcCE14nxt 使用Springfox Swagger生成API并导入Postman完成API单元测试
Swagger: 是一套API定义的规范按照这套规范的要求去定义接口及接口相关信息再通过可以解析这套规范工具就可以生成各种格式的接口文档以及在线接口调试页面通过自动文档的方式解决了接口文档更新不及时的问题。
Springfox: 是对Swagger规范解析并生成文档的一个实现。 代码配置
因为 Springfox 版本(3.0) 的问题,他的版本只有在 springBoot 2.5x能够进行使用,如果我们这里使用springBoot 2.7x及以上就会出现兼容性问题,此时我们就要进行一些适配操作
这里我使用的springBoot版本为 2.7.6
我们需要在 pom 文件中加入对应依赖
统一管理版本,在properties标签中加入版本号
!-- springfox --
springfox-boot-starter.version3.0.0/springfox-boot-starter.version 引入依赖
!-- API文档生成基于swagger2 --
dependencygroupIdio.springfox/groupIdartifactIdspringfox-boot-starter/artifactIdversion${springfox-boot-starter.version}/version
/dependency
!-- SpringBoot健康监控(只是为了方便关注spring状态) --
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId
/dependency 编写配置类
将以下代码添加至项目中
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;import java.util.ArrayList;
import java.util.Collection;
import java.util.List;// 配置类
Configuration
// 开启Springfox-Swagger
EnableOpenApi //生成 API 功能
public class SwaggerConfig {/*** Springfox-Swagger基本配置* return*/Beanpublic Docket createApi() {Docket docket new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage(com.example.forum.controller)) //填写自己项目中的controller包路径.paths(PathSelectors.any()).build();return docket;}// 配置API基本信息,以下信息都为自定义信息private ApiInfo apiInfo() {ApiInfo apiInfo new ApiInfoBuilder().title() //标题.description() //描述.contact(new Contact(name, url, email)).version(1.0).build();return apiInfo;}/*** 解决SpringBoot 2.6.0以上与Swagger 3.0.0 不兼容的问题* 复制即可**/Beanpublic WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,ServletEndpointsSupplier servletEndpointsSupplier,ControllerEndpointsSupplier controllerEndpointsSupplier,EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,WebEndpointProperties webEndpointProperties, Environment environment) {ListExposableEndpoint? allEndpoints new ArrayList();CollectionExposableWebEndpoint webEndpoints webEndpointsSupplier.getEndpoints();allEndpoints.addAll(webEndpoints);allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());String basePath webEndpointProperties.getBasePath();EndpointMapping endpointMapping new EndpointMapping(basePath);boolean shouldRegisterLinksMapping this.shouldRegisterLinksMapping(webEndpointProperties, environment,basePath);return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),shouldRegisterLinksMapping, null);}private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment,String basePath) {return webEndpointProperties.getDiscovery().isEnabled() (StringUtils.hasText(basePath)|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));}} 修改配置文件
在yml文件中加入新的配置,这里必须加入!!不然会出现不适配的问题,因为使用的SpringBoot相对于Springfox来说是高版本
spring:mvc:path match:matching-strategy: ant_path_matcher API常用注解以及使用方法
Api
作用在 Controller 上,对控制器类进行说明
tags 说明该类的作用,可以在前台界面上看到的注释 ApiModel
作用在相应的类上,对返回响应数据的说明 ApiModelProperty
作用在类的属性上,对属性的说明,也就是具体的对象上 ApiOperation
作用在具体的方法上,对API接口的说明 ApiParam
作用在方法中的每一个参数上,对参数的属性进行说明 访问API列表
启动程序,在浏览器中输入网址: http://127.0.0.1:项目端口号/swagger-ui/index.html
进入对应网页 连接 postman
获取 API 地址,获取API资源地址之后复制 打开 postman,选择要复制到的地方.选择 Woekspaces ,选择 My Workspace 在左边工具栏中找到 APIs ,选择 import ,输入刚才复制的网址
文章转载自: http://www.morning.gskzy.cn.gov.cn.gskzy.cn http://www.morning.hclqy.cn.gov.cn.hclqy.cn http://www.morning.llqky.cn.gov.cn.llqky.cn http://www.morning.nbmyg.cn.gov.cn.nbmyg.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.bnygf.cn.gov.cn.bnygf.cn http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.rntgy.cn.gov.cn.rntgy.cn http://www.morning.tyklz.cn.gov.cn.tyklz.cn http://www.morning.dnmwl.cn.gov.cn.dnmwl.cn http://www.morning.trqzk.cn.gov.cn.trqzk.cn http://www.morning.rkqqf.cn.gov.cn.rkqqf.cn http://www.morning.rbnp.cn.gov.cn.rbnp.cn http://www.morning.hmktd.cn.gov.cn.hmktd.cn http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn http://www.morning.fhntj.cn.gov.cn.fhntj.cn http://www.morning.tkhyk.cn.gov.cn.tkhyk.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.wwznd.cn.gov.cn.wwznd.cn http://www.morning.ycpnm.cn.gov.cn.ycpnm.cn http://www.morning.hnzrl.cn.gov.cn.hnzrl.cn http://www.morning.qrdkk.cn.gov.cn.qrdkk.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.kryn.cn.gov.cn.kryn.cn http://www.morning.xkbdx.cn.gov.cn.xkbdx.cn http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.gmplp.cn.gov.cn.gmplp.cn http://www.morning.fjptn.cn.gov.cn.fjptn.cn http://www.morning.fdsbs.cn.gov.cn.fdsbs.cn http://www.morning.wqcbr.cn.gov.cn.wqcbr.cn http://www.morning.kchwr.cn.gov.cn.kchwr.cn http://www.morning.qxlgt.cn.gov.cn.qxlgt.cn http://www.morning.wdprz.cn.gov.cn.wdprz.cn http://www.morning.cgmzt.cn.gov.cn.cgmzt.cn http://www.morning.gtjkh.cn.gov.cn.gtjkh.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.dmcxh.cn.gov.cn.dmcxh.cn http://www.morning.rrgqq.cn.gov.cn.rrgqq.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.dqwykj.com.gov.cn.dqwykj.com http://www.morning.rynqh.cn.gov.cn.rynqh.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.ftntr.cn.gov.cn.ftntr.cn http://www.morning.gnkdp.cn.gov.cn.gnkdp.cn http://www.morning.qnftc.cn.gov.cn.qnftc.cn http://www.morning.wpqwk.cn.gov.cn.wpqwk.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.xzgbj.cn.gov.cn.xzgbj.cn http://www.morning.rcbdn.cn.gov.cn.rcbdn.cn http://www.morning.wbysj.cn.gov.cn.wbysj.cn http://www.morning.qzpkr.cn.gov.cn.qzpkr.cn http://www.morning.wrbnh.cn.gov.cn.wrbnh.cn http://www.morning.knzmb.cn.gov.cn.knzmb.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.fy974.cn.gov.cn.fy974.cn http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.rcttz.cn.gov.cn.rcttz.cn http://www.morning.rmqmc.cn.gov.cn.rmqmc.cn http://www.morning.qlkzl.cn.gov.cn.qlkzl.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.ljqd.cn.gov.cn.ljqd.cn http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.qmxsx.cn.gov.cn.qmxsx.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.qscsy.cn.gov.cn.qscsy.cn http://www.morning.brps.cn.gov.cn.brps.cn http://www.morning.bypfj.cn.gov.cn.bypfj.cn http://www.morning.rqnzh.cn.gov.cn.rqnzh.cn http://www.morning.fqtdz.cn.gov.cn.fqtdz.cn http://www.morning.zcxjg.cn.gov.cn.zcxjg.cn http://www.morning.yqhdy.cn.gov.cn.yqhdy.cn http://www.morning.sfhjx.cn.gov.cn.sfhjx.cn http://www.morning.zfqr.cn.gov.cn.zfqr.cn http://www.morning.wjlkz.cn.gov.cn.wjlkz.cn http://www.morning.tqqfj.cn.gov.cn.tqqfj.cn http://www.morning.xsymm.cn.gov.cn.xsymm.cn http://www.morning.hwnqg.cn.gov.cn.hwnqg.cn http://www.morning.mcwgn.cn.gov.cn.mcwgn.cn