SpringBoot集成Swagger-Bootstrap-UI,页面更清爽!

沙海 2021年5月27日01:27:01Java评论39字数 3846阅读12分49秒阅读模式
摘要

SpringBoot集成Swagger-Bootstrap-UI,页面更清爽! 架构师专栏

SpringBoot集成Swagger-Bootstrap-UI,页面更清爽!

架构师专栏 文章源自JAVA秀-https://www.javaxiu.com/26298.html

大家好,我是磊哥。文章源自JAVA秀-https://www.javaxiu.com/26298.html

之前在创业公司待的时候,用过swagger,因为我第一天来这家公司工作,第一个任务就是做接口文档自动化。后来觉得它不太好用,在浏览技术网站的时候,偶然发现swagger-bootstrap-ui,于是便重构了,把swagger-bootstrap-ui整合进来,后来发现不仅仅对我们后端有帮助,主要方便我们将接口进行归类,同样对安卓小伙伴也有帮助,他们可以看这个接口文档进行联调。当初我使用swagger-boostrap-ui的时候,那个时候还是1.x版本,如今swagger-bootsrap-ui到2.x,同时也更改名字knife4j,适用场景从过去的单体到微服务。也算是见证咱们国人自己的开源项目从小到大。文章源自JAVA秀-https://www.javaxiu.com/26298.html

该开源项目GitHub地址:文章源自JAVA秀-https://www.javaxiu.com/26298.html

https://github.com/xiaoymin/Swagger-Bootstrap-UI文章源自JAVA秀-https://www.javaxiu.com/26298.html

注 意文章源自JAVA秀-https://www.javaxiu.com/26298.html

文末放有:7701页最新面试题文章源自JAVA秀-https://www.javaxiu.com/26298.html

该开源项目中文文档地址:文章源自JAVA秀-https://www.javaxiu.com/26298.html

https://doc.xiaominfo.com/文章源自JAVA秀-https://www.javaxiu.com/26298.html

一、添加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;@EnableSwagger2@EnableSwaggerBootstrapUIpublic 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 即可文章源自JAVA秀-https://www.javaxiu.com/26298.html

效果图,如下:文章源自JAVA秀-https://www.javaxiu.com/26298.html

SpringBoot集成Swagger-Bootstrap-UI,页面更清爽!文章源自JAVA秀-https://www.javaxiu.com/26298.html

测试接口,效果图如下:文章源自JAVA秀-https://www.javaxiu.com/26298.html

SpringBoot集成Swagger-Bootstrap-UI,页面更清爽!文章源自JAVA秀-https://www.javaxiu.com/26298.html

调式相当于用PostMan测试接口。文章源自JAVA秀-https://www.javaxiu.com/26298.html

四、常用注解

和swagger一样,swagger用的注解,swagger-bootstrap-ui仍能用。不过结合我的开发经验来看,最常用的也就两个,@Api和@ApiOperation。@Api的效果,如图:文章源自JAVA秀-https://www.javaxiu.com/26298.html

SpringBoot集成Swagger-Bootstrap-UI,页面更清爽!文章源自JAVA秀-https://www.javaxiu.com/26298.html

@ApiOperation的效果,如图:文章源自JAVA秀-https://www.javaxiu.com/26298.html

SpringBoot集成Swagger-Bootstrap-UI,页面更清爽!文章源自JAVA秀-https://www.javaxiu.com/26298.html

由此,我们很容易就看出来,它们的含义是什么,一个是接口分类说明,一个是接口方法说明。文章源自JAVA秀-https://www.javaxiu.com/26298.html

至于这里不用swagger的参数注解,主要原因是不想加太多的注解从而增加代码的数量,造成太多冗余。搜索Java知音公众号,回复“后端面试”,送你一份Java面试题宝典.pdf文章源自JAVA秀-https://www.javaxiu.com/26298.html

例子中的Controller代码:文章源自JAVA秀-https://www.javaxiu.com/26298.html

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;@RequestMapping("/user")@Api(tags = {"用户管理"}, description = "用户管理")public class UserController {    @Autowired private UsersService usersService;    @GetMapping("/list")    @ApiOperation(value = "用户列表")    public List<Users> list() {        return usersService.list();    }}

可能遇到的问题

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

一般是被拦截了(shiro或springsecurity机制)或者是配置错误。文章源自JAVA秀-https://www.javaxiu.com/26298.html

2.访问接口文档界面出来了,但扫描不到接口

主要是配置类的缘故,配置类有个包扫描,必须配置为controller路径。如图所示:文章源自JAVA秀-https://www.javaxiu.com/26298.html

SpringBoot集成Swagger-Bootstrap-UI,页面更清爽!文章源自JAVA秀-https://www.javaxiu.com/26298.html

如果还有其它问题,可以去官方文档上找,官方文档有一个常规问题列表和解决方案,如图所示:文章源自JAVA秀-https://www.javaxiu.com/26298.html

SpringBoot集成Swagger-Bootstrap-UI,页面更清爽!文章源自JAVA秀-https://www.javaxiu.com/26298.html

如果问题非常奇葩的话,实在解决不了(在参考官方文档说明和搜索的前提下,仍解决不了,把问题详细描述和关键性代码提到该开源项目的issue上,向创造者求助)。文章源自JAVA秀-https://www.javaxiu.com/26298.html

近期技术热文文章源自JAVA秀-https://www.javaxiu.com/26298.html

1、CTO说:Service 层和 Dao 的接口是不是多此一举?2、Spring 官宣,换掉 JVM,用上 GraalVM3、免费的XShell替代品,又来一款国产良心工具....4、拒绝 kill -9 暴力停止,优雅停止 SpringBoot 服务文章源自JAVA秀-https://www.javaxiu.com/26298.html

第3版:互联网大厂面试题文章源自JAVA秀-https://www.javaxiu.com/26298.html

包括 Java 集合、JVM、多线程、并发编程、设计模式、算法调优、Spring全家桶、Java、MyBatis、ZooKeeper、Dubbo、Elasticsearch、Memcached、MongoDB、Redis、MySQL、RabbitMQ、Kafka、Linux、Netty、Tomcat、Python、HTML、CSS、Vue、React、JavaScript、Android 大数据、阿里巴巴等大厂面试题等、等技术栈!文章源自JAVA秀-https://www.javaxiu.com/26298.html

阅读原文: 高清 7701页大厂面试题  PDF文章源自JAVA秀-https://www.javaxiu.com/26298.html

阅读原文文章源自JAVA秀-https://www.javaxiu.com/26298.html

继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定