SpringCloud之使用Swagger生成微服务中API文档

随着微服务架构体系的发展和应用, 为了前后端能够更好的集成与对接,同时为了项目的方便交付,每个项目都需要提供相应的API文档。

来源:PC端、微信端、H5端、移动端(安卓和IOS端)

 

传统的API文档编写存在以下几个痛点:

对API文档进行更新的时候,需要通知前端开发人员,导致文档更新交流不及时;

API接口返回信息不明确

大公司中肯定会有专门文档服务器对接口文档进行更新。

缺乏在线接口测试,通常需要使用相应的API测试工具,比如postman、SoapUI等

接口文档太多,不便于管理

为了解决传统API接口文档维护的问题,为了方便进行测试后台Restful接口并实现动态的更新,因而引入Swagger接口工具。

 

Swagger具有以下优点

1.功能丰富:支持多种注解,自动生成接口文档界面,支持在界面测试API接口功能;

2.及时更新:开发过程中花一点写注释的时间,就可以及时的更新API文档,省心省力;

3.整合简单:通过添加pom依赖和简单配置,内嵌于应用中就可同时发布API接口文档界面,不需要部署独立服务。

 

Swagg  er 2.0 集成配置

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.buba</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <!-- SpringBoot整合Web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- SpringBoot整合eureka客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>
    </dependencies>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <!--Finchley.RELEASE 加上这个东西,别使用默认的  或者使用Finchley.M7这个兼容springcloud2.0版本但是我使用不好使-->
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <!--加上这个false就不从上面这个链接里下载jar包-->
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

写一个配置类,描述文档一些信息

package com.buba.configuration;

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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				// 扫包的范围,也就是把哪些包生成api文档
				.apis(RequestHandlerSelectors.basePackage("com.buba.controller")).paths(PathSelectors.any()).build();
	}

	private ApiInfo apiInfo() {
		//title文档标题
		//description文档描述
		//termsOfServiceUrl自定义地址
		//version版本号
		return new ApiInfoBuilder().title("微服务电商系统").description("微服务swagger")
				.termsOfServiceUrl("http://www.buba.com")
				// .contact(contact)
				.version("1.0").build();
	}

}

随便写个controller接口,然后启动项目进行测试,启动后报的错不用管.

package com.buba.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Api("aaa")
@Controller
public class SwaggerController {

    //描述方法的
    @ApiOperation("方法一描述")
    //描述参数的  name参数名  value描述信息  required是否必填  dataType参数类型
    @ApiImplicitParam(name = "userName",value = "用户名参数",required = true,dataType = "String")
    @GetMapping("/swaggerIndex")
    public String swaggerIndex(String userName){
        System.out.println(userName);
        return "index";
    }
}

http://localhost:9090/swagger-ui.html访问这个路径就能看到api文档详细信息了

直接在这上面就可以测试

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
若依微服务是一种基于Spring Cloud的微服务架构,它提供了一套完整的解决方案来快速构建和部署分布式系统。Swagger文档是一种用于描述和展示API接口的工具,可以方便开发人员和测试人员理解和调用接口。 在若依微服务,可以通过更换Swagger文档来实现以下几个方面的需求: 1. 支持更多的API文档格式:目前,Swagger文档使用的是OpenAPI规范,可以方便地生成和展示API接口文档。但是有时候,我们可能需要支持其他类型的API文档格式,比如RAML、API Blueprint等。通过更换Swagger文档,我们可以灵活选择适合自己的API文档格式。 2. 自定义API文档样式:Swagger文档默认提供了一套简洁的样式和布局,但是它可能不符合我们的品牌和设计要求。通过更换Swagger文档,我们可以自定义API文档的样式,包括颜色、字体、布局等,以适应我们的品牌形象。 3. 扩展API文档功能:Swagger文档提供了一些基本的功能,比如生成API文档、调试接口、测试接口等。但是在实际应用,我们可能需要更多的功能来满足特定的需求,比如权限控制、数据模型关联等。通过更换Swagger文档,我们可以集成其他的API文档工具或者自行开发插件来扩展API文档的功能。 总之,若依微服务更换Swagger文档可以帮助我们实现更多定制化和扩展性的需求,从而更好地满足项目的特定要求。通过更换Swagger文档,我们可以选择适合自己的API文档格式、样式和功能,提高团队的开发效率和项目的可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值