swagger2-实时生成接口文档

1.介绍swagger2
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。
在这里插入图片描述
2.引入Swagger2和swagger ui

 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

3.编写配置类

package com.offcn.user.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.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class AppSwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.offcn.user.controller")) // swagger2 测试类
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("七易众筹-系统平台接口文档") //大标题
                .description("提供用户模块的文档") //提示信息
                .termsOfServiceUrl("http://www.ujiuye.com/")
                .version("1.0") //版本
                .build();
    }
}

4.使用注解

使用Swagger注解
@Api(tags="")

用在请求的类上,表示对类的说明
tags"说明该类的作用,可以在UI界面上看到的注解"

@ApiOperation(value="")
用在请求的方法上,说明方法的用途、作用
value="说明方法的用途、作用"

@ApiImplicitParams
用在请求的方法上,表示一组参数说明

@ApiImplicitParam:
指定一个请求参数的各个方面
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪个地方
header –> 请求头的获取:@RequestHeader
query –> 请求参数的获取:@RequestParam
path(用于restful接口)–> 请求路径变量的获取:@PathVariable
body(不常用)
form(不常用)
dataType:参数类型,默认String,其它值dataType="Integer"
defaultValue:参数的默认值

@ApiResponses
用在请求的方法上,表示一组响应

@ApiResponse
用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类

@ApiModel
主要有两种用途:
用于响应类上,表示一个返回响应数据的信息
入参实体:使用@RequestBody这样的场景, 请求参数无法使用

@ApiImplicitParam注解进行描述的时候

@ApiModelProperty
用在属性上,描述响应类的属性

5.编写pojo文件

package com.offcn.user.bean;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel("测试实体")
public class User {

    @ApiModelProperty(value = "主键")
    private int id;
    @ApiModelProperty(value = "姓名")
    private String name;
    @ApiModelProperty(value = "电子邮件")
    private String email;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

6.编写测试类

package com.offcn.user.controller;

import com.offcn.user.bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "第一个Swagger测试")
@RestController
public class HelloController {
    @ApiOperation("测试方法hello")
    @ApiImplicitParams(value={
            @ApiImplicitParam(name="name",value="姓名",required = true),@ApiImplicitParam(name="age",value="年龄")
    })
    @GetMapping("/hello")
    public String hello(String name){
        return "OK:"+name;
    }

    @ApiOperation("保存用户")
    @ApiImplicitParams(value={
            @ApiImplicitParam(name="name",value="姓名",required = true),@ApiImplicitParam(name="email",value="电子邮件")
    })
    @PostMapping("/user")
    public User save(String name, String email){
        User user  = new User();
        user.setName(name);
        user.setEmail(email);
        return user;
    }
}

http://localhost:7000/swagger-ui.html
在这里插入图片描述
7.主配置类添加注解

@EnableSwagger2  //开启swagger2
Swagger UI是一款RESTFUL接口的文档在线自动生成 功能测试功能软件。       现在多数的项目开发中,网站和移动端都需要进行数据交互和对接,这少不了使用REST编写API接口这种场景。例如有些团队,移动端交由了另一团队开发,不同开发小组之间就需要以规范和文档作为标准和协作基础。良好的文档可以让开发事半功倍,而作为又懒又要效率又能交代的码农,当然最希望一 切自动化,或用小聪明来找到最适合的工具。       Swagger-UI简单而一目了然。它能够纯碎的基于html javascript实现,只要稍微整合一下便能成为方便的API在线测试工具。       项目的设计架构中一直提倡使用TDD(测试驱动)原则来开发,swagger-ui在这方面更是能提供很大帮助。 Swagger-UI更倾向于在线测试接口和数据,但其核心是一个javascript插件,只要稍作修改,便能按需求定制出不同格式的说明文档,在github上更是基于它集成到各种语言环境,分支众多。        其官方提供了一个离线版本,它的使用方法十分简单:直接在js格式的资源文件中录入REST API的json信息,便能容易地生成不同模块下的API列表,每个API接口描述和参数、请求方法都能在每个json数组中定制。下面是目前项目中使用到的部分预览图:  Swagger-UI 的官方地址: http://swagger.wordnik.com Github上的项目地址: https://github.com/wordnik/swagger-ui 官方提供的demo地址 http://petstore.swagger.wordnik.com/ 标签:api
Swagger Maven Plugin是一个用于生成Swagger接口文档的Maven插件。它可以帮助开发人员在构建项目时自动生成Swagger规范的JSON或YAML文件,以便于API文档的管理和使用。 使用Swagger Maven Plugin生成接口文档swagger.json或swagger.yaml的步骤如下: 1. 在项目的pom.xml文件中添加Swagger Maven Plugin的依赖配置: ```xml <build> <plugins> <plugin> <groupId>com.github.kongchen</groupId> <artifactId>swagger-maven-plugin</artifactId> <version>3.1.8</version> <configuration> <!-- 配置Swagger文档的基本信息 --> <apiSources> <apiSource> <springmvc>true</springmvc> <locations>com.example.controller</locations> <basePath>/api</basePath> <info> <title>API文档</title> <version>1.0.0</version> <description>API接口文档</description> <termsOfServiceUrl>http://example.com/terms-of-service</termsOfServiceUrl> <contact> <email>contact@example.com</email> </contact> <license> <name>Apache 2.0</name> <url>http://www.apache.org/licenses/LICENSE-2.0.html</url> </license> </info> </apiSource> </apiSources> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 2. 在项目根目录下执行以下命令生成Swagger接口文档: ``` mvn compile swagger:generate ``` 3. 执行完上述命令后,Swagger Maven Plugin会根据配置的信息扫描项目中的接口,并生成Swagger规范的JSON或YAML文件。生成的文件默认保存在项目的target目录下的swagger目录中。 生成Swagger接口文档可以通过访问http://localhost:8080/api/swagger-ui.html(假设项目部署在本地的8080端口)来查看和测试API接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值