300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 你还在手写接口文档?来 壹哥教你一招实现接口文档

你还在手写接口文档?来 壹哥教你一招实现接口文档

时间:2021-02-21 21:16:05

相关推荐

你还在手写接口文档?来 壹哥教你一招实现接口文档

我们知道,现在很多项目开发都采用了前后端分离的模式。在这种模式下,前端人员开发前端相关的功能,后端人员开发后端相关的功能。那么问题来了,前端需要调用后端实现的接口进行交互,两者之间是如何进行交互的?前端怎么知道后端编写的接口在哪?该传递哪些参数?返回值是什么?这些在前后端分离时都是问题!

那么这些问题在实际开发时该怎么解决呢?今天壹哥就给大家来聊聊,项目中是怎么编写接口文档的。

一. 前言

既然现在的项目开发很多都是采用前后端分离的模式,那么前端和后端的交互联系,就得依靠API接口文档来完成。因此API接口文档就变得越来越重要。Swagger就是一个方便我们更好地编写API文档的框架,而且Swagger还带有接口测试功能。接下来壹哥就通过一篇文章,来教会大家怎么使用Swagger文档。

二. Swagger使用步骤

话不多说,我们直接上实现教程。

1. 导入jar

<!-- 该jar包依赖springfox3.X 无需额外引入--><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0.3</version></dependency>

2. 相关配置

2.1 application.yml配置文件

首先我们在application.yml文件中添加相关的配置信息。

# Swagger配置swagger:# 是否开启swaggerenabled: true

注意:我们一般不会在生产环境中使用swagger,所以可以把enabled设置为false。

2.2 Swagger配置类

配置类中需要使用@EnableOpenApi注解进行修饰。

/*** Swagger3的接口配置* <p>* http://localhost:8080/swagger-ui/index.html#/*/@Configuration@EnableOpenApipublic class SwaggerConfig {/*** 是否开启swagger*/@Value("${swagger.enabled}")private boolean enabled;/*** 创建API*/@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.OAS_30)// 是否启用Swagger.enable(enabled)// 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息).apiInfo(apiInfo())// 设置哪些接口暴露给Swagger展示.select()// 扫描所有有注解的api,用这种方式更灵活.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))// 扫描指定包中的swagger注解//.apis(RequestHandlerSelectors.basePackage("com.qfedu"))// 扫描所有.paths(PathSelectors.any()).build()/* 设置安全模式,swagger可以设置访问token */.securitySchemes(securitySchemes()).securityContexts(securityContexts()).pathMapping("/");}/*** 安全模式,这里指定token通过Authorization头请求头传递*/private List<SecurityScheme> securitySchemes() {List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();apiKeyList.add(new ApiKey("token", "token", In.HEADER.toValue()));return apiKeyList;}/*** 安全上下文*/private List<SecurityContext> securityContexts() {List<SecurityContext> securityContexts = new ArrayList<>();securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth()).operationSelector(o -> o.requestMappingPattern().matches("/.*")).build());return securityContexts;}/*** 默认的安全上引用*/private List<SecurityReference> defaultAuth() {AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];authorizationScopes[0] = authorizationScope;List<SecurityReference> securityReferences = new ArrayList<>();securityReferences.add(new SecurityReference("Authorization", authorizationScopes));return securityReferences;}/*** 添加摘要信息*/private ApiInfo apiInfo() {// 用ApiInfoBuilder进行定制return new ApiInfoBuilder()// 设置标题.title("app接口文档")// 描述.description("具体包括XXX,XXX模块...")// 作者信息.contact(new Contact("qfedu", null, null))// 版本.version("1.0.0").build();}}

2.3 swagger静态资源的映射处理

因为在Swagger框架中存在一些静态的资源文件,我们需要把这些静态资源在过滤器中放行。

@Configurationpublic class WebMvcConfigurer extends WebMvcConfigurationSupport {/*** 继承了WebMvcConfigurationSupport,重新指定静态资源*/@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");super.addResourceHandlers(registry);}}

3. 接口调用

我们可以打开如下地址进行接口调用:http://localhost:8010/doc.html

注意,http://localhost:8010/swagger-ui/index.html无法使用,是由于knife4j-spring-boot-starter中排除了springfox-swagger-ui的jar包。如果我们想使用http://localhost:8010/swagger-ui/index.html ,还需要额外导入一个jar包。

<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>3.0.0</version></dependency>

三. 设置接口文档的用户名和密码

出于安全考虑,我们在application.yml文件中,应该设置访问swagger时的用户名和密码。

knife4j:enable: truebasic:enable: trueusername: adminpassword: dic13579

四. 总结

好了,今天的文章就到这里,现在你学会怎么使用swagger在线文档了吗?如果你还有其他任何问题,可以私信壹哥,我来教你怎么具体使用。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。