From 529a0a755cd530c009a88c63f7a1e6f481b7c5f9 Mon Sep 17 00:00:00 2001
From: zyx <1029606625@qq.com>
Date: Tue, 28 Feb 2023 18:49:53 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8Knife4j=E5=8A=A0=E5=BC=BA?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0=E7=9A=84Swagger=E7=94=9F=E6=88=90API?=
=?UTF-8?q?=E6=96=87=E6=A1=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 5 +++
.../czyx007/reggie/config/WebMvcConfig.java | 32 +++++++++++++++++++
.../reggie/filter/LoginCheckFilter.java | 6 ++--
3 files changed, 39 insertions(+), 4 deletions(-)
diff --git a/pom.xml b/pom.xml
index 250aec7..f09dccb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -37,6 +37,11 @@
sharding-jdbc-spring-boot-starter
4.1.1
+
+ com.github.xiaoymin
+ knife4j-spring-boot-starter
+ 3.0.3
+
org.springframework.boot
spring-boot-starter-test
diff --git a/src/main/java/cn/czyx007/reggie/config/WebMvcConfig.java b/src/main/java/cn/czyx007/reggie/config/WebMvcConfig.java
index 9fb45f0..5b09911 100644
--- a/src/main/java/cn/czyx007/reggie/config/WebMvcConfig.java
+++ b/src/main/java/cn/czyx007/reggie/config/WebMvcConfig.java
@@ -1,11 +1,20 @@
package cn.czyx007.reggie.config;
import cn.czyx007.reggie.common.JacksonObjectMapper;
+import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
+import org.springframework.context.annotation.Bean;
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 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;
import java.util.List;
@@ -14,13 +23,36 @@ import java.util.List;
* @createTime : 2022/12/20 - 19:54
*/
@Configuration
+@EnableSwagger2
+@EnableKnife4j
public class WebMvcConfig extends WebMvcConfigurationSupport {
+ private ApiInfo apiInfo(){
+ return new ApiInfoBuilder()
+ .title("瑞吉外卖")
+ .version("1.0")
+ .description("瑞吉外卖接口文档")
+ .build();
+ }
+
+ @Bean
+ public Docket createRestApi(){
+ //文档类型
+ return new Docket(DocumentationType.SWAGGER_2)
+ .apiInfo(apiInfo())
+ .select()
+ .apis(RequestHandlerSelectors.basePackage("cn.czyx007.reggie.controller"))
+ .paths(PathSelectors.any())
+ .build();
+ }
+
/**
* 设置静态资源映射
* @param registry
*/
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
+ registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
+ registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
registry.addResourceHandler("/backend/**").addResourceLocations("classpath:/static/backend/");
registry.addResourceHandler("/front/**").addResourceLocations("classpath:/static/front/");
}
diff --git a/src/main/java/cn/czyx007/reggie/filter/LoginCheckFilter.java b/src/main/java/cn/czyx007/reggie/filter/LoginCheckFilter.java
index b9a1d17..44310d3 100644
--- a/src/main/java/cn/czyx007/reggie/filter/LoginCheckFilter.java
+++ b/src/main/java/cn/czyx007/reggie/filter/LoginCheckFilter.java
@@ -1,10 +1,8 @@
package cn.czyx007.reggie.filter;
-import cn.czyx007.reggie.bean.Employee;
import cn.czyx007.reggie.common.BaseContext;
import cn.czyx007.reggie.common.R;
import com.alibaba.fastjson.JSON;
-import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.util.AntPathMatcher;
import javax.servlet.*;
@@ -21,8 +19,8 @@ import java.io.IOException;
@WebFilter(filterName = "loginCheckFilter", urlPatterns = "/*")
public class LoginCheckFilter implements Filter {
private static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
- public static final String[] urls = {"/employee/login", "/employee/logout", "/backend/**", "/front/**",
- "/user/sendMsg", "/user/login"};
+ private static final String[] urls = {"/employee/login", "/employee/logout", "/backend/**", "/front/**",
+ "/user/sendMsg", "/user/login", "/doc.html", "/webjars/**", "/swagger-resources", "/v2/api-docs"};
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {