关于java:SpringBoot集成SwaggerBootstrapUI

19次阅读

共计 3334 个字符,预计需要花费 9 分钟才能阅读完成。

之前在守业公司待的时候,用过 swagger,因为我第一天来这家公司工作,第一个工作就是做接口文档自动化。

起初感觉它不太好用,在浏览技术网站的时候,偶尔发现 swagger-bootstrap-ui,于是便重构了,把 swagger-bootstrap-ui 整合进来,起初发现不仅仅对咱们后端有帮忙,次要不便咱们将接口进行归类,同样对安卓小伙伴也有帮忙,他们能够看这个接口文档进行联调。当初我应用 swagger-boostrap-ui 的时候,那个时候还是 1.x 版本,现在 swagger-bootsrap-ui 到 2.x,同时也更改名字 knife4j,实用场景从过来的单体到微服务。也算是见证咱们国人本人的开源我的项目从小到大。

该开源我的项目 GitHub 地址:
https://github.com/xiaoymin/S…

该开源我的项目中文文档地址:
https://doc.xiaominfo.com/

一、增加 Maven 依赖

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

二、增加配置类

package com.blog.tutorial.config;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
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;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 15:46
 */@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfiguration {
    @Bean
 public Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.blog.tutorial.controller"))
                .paths(PathSelectors.any())
                .build();}
    private ApiInfo apiInfo() {return new ApiInfoBuilder()
                .title("swagger-bootstrap-ui RESTful APIs")
                .description("swagger-bootstrap-ui")
                .termsOfServiceUrl("http://localhost:5050/")
                .contact("developer@mail.com")
                .version("1.0")
                .build();}
}

三、启动我的项目

启动我的项目,不报错,而后拜访地址:
http://ip:port/doc.html 即可

效果图,如下:

测试接口,效果图如下:

调式相当于用 PostMan 测试接口。

四、罕用注解

和 swagger 一样,swagger 用的注解,swagger-bootstrap-ui 仍能用。
不过联合我的开发教训来看,最罕用的也就两个,@Api 和 @ApiOperation。
@Api 的成果,如图:

@ApiOperation 的成果,如图:

由此,咱们很容易就看进去,它们的含意是什么,一个是接口分类阐明,一个是接口办法阐明。

至于这里不必 swagger 的参数注解,次要起因是不想加太多的注解从而减少代码的数量,造成太多冗余。

例子中的 Controller 代码:

package com.blog.tutorial.controller;
import com.blog.tutorial.entity.Users;
import com.blog.tutorial.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 13:27
 */@RestController
@RequestMapping("/user")
@Api(tags = {"用户治理"}, description = "用户治理")
public class UserController {
    @Autowired
 private UsersService usersService;
    @GetMapping("/list")
    @ApiOperation(value = "用户列表")
    public List<Users> list() {return usersService.list();
    }
}

五、其它

对于 swagger 整合系列,能够参考如下:
MP 实战系列 \(二 \)之集成 swagger

对于 swagger-bootstrap 整合系列,能够参考:

MP 实战系列 \(八 \)之 SpringBoot+Swagger2

springfox-swagger 之 swagger-bootstrap-ui

六、可能遇到的问题

1. 拜访不到接口文档界面白版

个别是被拦挡了 (shiro 或 springsecurity 机制) 或者是配置谬误。

2. 拜访接口文档界面进去了,但扫描不到接口

次要是配置类的缘故,配置类有个包扫描,必须配置为 controller 门路。
如图所示:

如果还有其它问题,能够去官网文档上找,官网文档有一个惯例问题列表和解决方案,如图所示:

如果问题十分奇葩的话,切实解决不了(在参考官网文档阐明和搜寻的前提下,仍解决不了,把问题详细描述和关键性代码提到该开源我的项目的 issue 上,向创造者求助)。

正文完
 0