knife4j 配置

依赖导入

  • springboot2
1
2
3
4
5
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
<version>4.1.0</version>
</dependency>
  • springboot3
1
2
3
4
5
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
<version>4.3.0</version>
</dependency>

配置

  • springboot2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
knife4j:
enable: true
openapi:
title: 黑马商城接口文档
description: "黑马商城接口文档"
email: zhanghuyi@itcast.cn
concat: 虎哥
url: https://www.itcast.cn
version: v1.0.0
group:
default:
group-name: default
api-rule: package
api-rule-resources:
- com.genshin.ojuser.controller
  • springboot3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
springdoc:
swagger-ui:
path: /swagger-ui.html
tags-sorter: alpha
operations-sorter: alpha
api-docs:
path: /v3/api-docs
group-configs:
- group: 'default'
paths-to-match: '/**'
packages-to-scan: com.homework.genshinchat
# knife4j的增强配置,不需要增强可以不配
knife4j:
enable: true
setting:
language: zh_cn
  • 测试

http://localhost:8081/doc.html#/home

语法规则

这个可以到网上找。

springmvc

  • 如果你的项目是一个springmvc项目,那么需要下面这么做
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.itheima.reggie_take_out.config;

import com.itheima.reggie_take_out.common.JacksonObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
/**
* 设置静态资源映射
* @param registry
*/
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
log.info("开始进行静态资源服务");
registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/");
registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/");
//registry.addResourceHandler("/swagger-ui.html/**").addResourceLocations("classpath:/swagger-ui.html/");
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
}
}

@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
// 创建消息转换器对象
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();

//设置对象转换器, 底层使用 Jackson将java对象转换为 json
messageConverter.setObjectMapper(new JacksonObjectMapper());

// 将上面的消息转换器追加到mvc框架的转换器集合中
converters.add(0, messageConverter);
// super.extendMessageConverters(converters);
}
}

配置前

image-20231202221034183

出现空白页

发现是被拦截了

image-20231202221135125

那么放开这个/webjars 对应的拦截就好了

"/webjars/css/chunk-296622eb.20e6d994.css"

1
2
3
4
5
6
"/swagger-ui.html/**",
"/Swagger-ui.html/**",
"/doc.html/**",
"/v2/**",
"/webjars/**",
"/swagger-resources/**"