knife4j 配置
依赖导入
1 2 3 4 5
| <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-openapi2-spring-boot-starter</artifactId> <version>4.1.0</version> </dependency>
|
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>
|
配置
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
|
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: 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;
@Slf4j @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { log.info("开始进行静态资源服务"); registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/backend/"); registry.addResourceHandler("/front/**").addResourceLocations("classpath:/front/"); 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();
messageConverter.setObjectMapper(new JacksonObjectMapper());
converters.add(0, messageConverter); } }
|
配置前
出现空白页
发现是被拦截了
那么放开这个/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/**"
|