低版的spring boot 1.X接入knife4j

低版的spring boot 1.X接入knife4j

老的项目是用spring boot 1.5.10.RELEASE 不好升级 ,原来接口文档一直用的是老的swagger样式,不是很好看,网上查了下,发现有个knife4j挺好看的,用一下他们的样式,下面是接入详细

Maven中引入Jar包

由于是springfox-swagger的增强UI包,所以基础功能依然依赖Swagger,springfox-swagger的jar包必须引入
io.springfox springfox-swagger2 2.9.2 然后引入SwaggerBootstrapUi的jar包 com.github.xiaoymin swagger-bootstrap-ui ${lastVersion} 编写Swagger2Config配置文件 Swagger2Config配置文件如下:

@Configuration

@EnableSwagger2

public class SwaggerConfiguration {

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo())

.select()

.apis(RequestHandlerSelectors.basePackage("com.bycdao.cloud"))

.paths(PathSelectors.any())

.build();

}

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

.title("swagger-bootstrap-ui RESTful APIs")

.description("swagger-bootstrap-ui")

.termsOfServiceUrl("http://localhost:8999/")

.contact("developer@mail.com")

.version("1.0")

.build();

}

}

访问地址

swagger-bootstrap-ui默认访问地址是:http:// h o s t : {host}: host:{port}/doc.html

注意事项

Springfox-swagger默认提供了两个Swagger接口,需要开发者放开权限(如果使用shiro权限控制框架等),如果使用SwaggerBootstrapUi的增强功能,还需放开增强接口地址,所以,放开的权限接口包括3个,分别是:

/swagger-resources:Swagger的分组接口

/v2/api-docs?group=groupName:Swagger的具体分组实例接口,返回该分组下所有接口相关的Swagger信息

/v2/api-docs-ext?group=groupName:该接口是SwaggerBootstrapUi提供的增强接口地址,如不使用UI增强,则可以忽略该接口

Shiro的相关配置实例如下:
/swagger-resources = anon /v2/api-docs = anon /v2/api-docs-ext = anon /doc.html = anon /webjars/** = anon

复制代码
    //others....
</value>    

SpringBoot中访问doc.html报404的解决办法

实现SpringBoot的WebMvcConfigurer接口,添加相关的ResourceHandler,代码如下:

@SpringBootApplication

@ConditionalOnClass(SpringfoxWebMvcConfiguration.class)

public class SwaggerBootstrapUiDemoApplication implements WebMvcConfigurer{

复制代码
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
	registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
	registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}

}

使用SpringMvc的朋友.在web.xml中配置了DispatcherServlet,则需要追加一个url匹配规则,如下
cmsMvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:config/spring.xml 1 cmsMvc *.htm cmsMvc /v2/api-docs cmsMvc /swagger-resources cmsMvc /v2/api-docs-ext

UI效果图

接口说明

接口调试

个性化设置

接口离线文档

相关推荐
AskHarries5 分钟前
Shell Tool:命令执行、输出读取和长任务管理
后端
张某布响丸辣6 分钟前
Spring AI 极简入门:Java 开发者快速上手 AI 开发
java·人工智能·spring·springai
java1234_小锋8 分钟前
请描述 Spring Boot 的启动流程,包括 SpringApplication 的初始化和 run 方法的核心步骤。
java·数据库·spring boot
疯狂成瘾者10 分钟前
Java 集合 LinkedList 详解:链表结构、常用方法和队列使用
java·开发语言·链表
lanyxp16 分钟前
Sentinel 管不到 SQL 这一层——我写了个 MyBatis SQL 熔断器
java
小闹54916 分钟前
Docker 如何才能学的更扎实
后端·程序员
武子康23 分钟前
Java-28 深入浅出 Spring 实现简易Ioc-04 在上节的业务下手动实现AOP
java·后端·mybatis
XovH27 分钟前
MySQL 系列:第10篇 存储过程与自定义函数
后端
XovH27 分钟前
MySQL 系列:第8篇 子查询与集合操作
后端