超级好用的API文档自动生成,谁用谁知道

1.swagger2 配置简介

    1.1 是一款让你更好的书写API文档的规范且完整框架。

    1.2 提供描述、生产、消费和可视化RESTful Web Service。

    1.3 可以提供在线接口调试,是前后端分离协作开发的利器。

2.springboot整合swagger2

    2.1 在pom.xml中添加依赖

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

为了API接口文档网页前端画面更炫酷,特此在pom.xml再添加以下依赖

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>swagger-bootstrap-ui</artifactId>
    <version>1.9.6</version>
</dependency>

2.2 配置swagger2

在你的common包中,添加如下配置类

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {

//    http://localhost:8088/swagger-ui.html     原路径,原生
//    http://localhost:8088/doc.html     原路径,github

    // 配置swagger2核心配置 docket
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)  // 指定api类型为swagger2
                .apiInfo(apiInfo())                 // 用于定义api文档汇总信息
                .select()
                .apis(RequestHandlerSelectors
                        .basePackage("com.htf.controller"))   // 指定controller包
                .paths(PathSelectors.any())         // 所有controller
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("天天吃货 电商平台接口api")        // 文档页标题
                .contact(new Contact("霍**",           // 作者名 
                        "https://pasturecloud.com/user",// 作者博客等等,无意义
                        "181461****@qq.com"))        // 联系人信息
                .description("专为天天吃货提供的api文档")  // 详细信息
                .version("1.0.1")   // 文档版本号
                .termsOfServiceUrl("https://pasturecloud.com/**") // 网站地址
                .build();
    }

}

3.相关注解使用

    3.1 用在controller类上

@Api(value = "注册登录", tags = {"用于登录注册的相关接口"})

    3.2 用在方法上

@ApiOperation(value = "用户名是否存在", notes = "判断用户名是否存在的API", httpMethod = "GET")

    3.3 用在方法参数上

public ResponseJSONResult setDefalut(
            @ApiParam(name = "userId", value = "用户ID", required = true)
            @RequestParam String userId,
            @ApiParam(name = "addressId", value = "地址ID", required = true)
            @RequestParam String addressId){
    //TODO 代码
}

    3.4 用在实体类以及其属性上

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;

@Data
@ApiModel(value = "用户VO对象", description = "用于前后端交互的数据对象")
public class UsersVO {
    /**
     * 用户名
     */
    @NotBlank(message = "用户名不能为空")
    @ApiModelProperty(value = "用户名",name = "username",example = "小马",required = true)
    private String username;

    /**
     * 密码
     */
    @NotBlank(message = "密码不能为空")
    @Length(min = 6,message = "密码长度不能小于6位")
    @ApiModelProperty(value = "密码",name = "password",example = "123123",required = true)
    private String password;

    /**
     * 确认密码
     */
    @NotBlank(message = "确认密码不能为空")
    @ApiModelProperty(value = "确认密码",name = "confirmPassword",example = "123123",required = false)
    private String confirmPassword;
}

4.API接口文档访问

原生API接口文档访问地址,你服务器ip地址:端口号/swagger-ui.html

炫酷的API接口文档访问地址,你服务器ip地址:端口号/doc.html

转载请注明原作地址!!!

发布了16 篇原创文章 · 获赞 13 · 访问量 3036

猜你喜欢

转载自blog.csdn.net/qq_34399639/article/details/105245856