引言
笔者本身算得上是一名全栈工程师,刚刚入行的时候去的一个小公司做前端,要问这前端最痛苦的事情是什么,那就是看后端给的文档没有之一。当时文档极度不规范,有的后台给word,有的给txt,规范上写的也不标准,不是漏写请求方式就是写错请求参数。。。
后来项目中集成了springfox-swagger-ui
可算是能避免上面的问题了,但是这个插件对于前端去看很不友好,也不是很方便,尤其是新手上手比较困难。
那么为了解决这些问题,我今天给大家介绍一个swagger-bootstrap-ui
的java ui 库。
使用
1. 添加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>
2. 添加配置类
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
@Bean
public Docket adminApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("adminApi")
.apiInfo(adminApiInfo())
.select()
//该位置输入需要检查的controller路径,可以添加多个。
.apis(RequestHandlerSelectors.basePackage("com.jxys.scaffold.user.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo adminApiInfo(){
return new ApiInfoBuilder()
.title("后台管理系统-API文档")
.description("本文档描述了后台管理系统微服务接口定义")
.version("1.0")
.contact(new Contact("admin", "http://localhost:8080", "88888@163.com"))
.build();
}
}
3.访问地址
启动项目,不报错,然后访问地址:
http://你项目的ip:你项目的端口/doc.html
即可。
怎么样,够细致吧。
一些安装问题的解决
这里分享一些使用插件时可能会出现的问题,以免大家掉入坑中。
1.访问不到接口文档界面白版
一般是被拦截了(shiro或springsecurity机制)或者是配置错误。
也有可能是你自己的拦截器进行了拦截。
2.访问接口文档界面出来了,但扫描不到接口
主要是配置类的缘故,配置类有个包扫描,必须配置为controller路径。
如图所示:
3.访问接口后报404
没有配置开放静态资源,需要这样做
@SpringBootConfiguration
public class InterceptorConfig implements WebMvcConfigurer {
//使这个拦截器提前加载,避免@Autowired无法获取到数据
@Bean
public LoginFailureInterceptor getLoginInterceptor(){
return new LoginFailureInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
/*//注册自己的拦截器,并设置拦截路径,拦截多个可以全一个list集合*/
//业务接口
List<String> excludeList=new ArrayList<>();
excludeList.add("/user/signIn");
excludeList.add("/user/signUp");
//组件接口
excludeList.add("/swagger-resources/**");
excludeList.add("/webjars/**");
excludeList.add("/v2/**");
excludeList.add("/error");
excludeList.add("/csrf");
excludeList.add("/");
excludeList.add("/swagger-ui.html/**");
excludeList.add("/doc.html/**");
registry.addInterceptor(getLoginInterceptor()).addPathPatterns("/**").excludePathPatterns(excludeList);
}
}
其他问题大家就只能去百度了。
另附:
演示开源项目脚手架GitHub地址:
https://github.com/a241978181/springboot-scaffold
Swagger-Bootstrap-UI项目GitHub地址:
https://github.com/xiaoymin/Swagger-Bootstrap-UI
Swagger-Bootstrap-UI项目中文文档地址:
https://doc.xiaominfo.com/
如果觉得有帮助的话给个免费的点赞吧,Thanks♪(・ω・)ノ