swagger ui 优化
无效类去除精简 修改用户上下文获取request/response的方式
This commit is contained in:
parent
444b617c63
commit
d432b0a183
@ -151,7 +151,7 @@ ignored:
|
|||||||
- /swagger-resources/**
|
- /swagger-resources/**
|
||||||
- /swagger/**
|
- /swagger/**
|
||||||
- /webjars/**
|
- /webjars/**
|
||||||
- /v2/api-docs
|
- /v2/api-docs**
|
||||||
- /configuration/ui
|
- /configuration/ui
|
||||||
- /boot-admin
|
- /boot-admin
|
||||||
- /manager/promotion/seckill/init
|
- /manager/promotion/seckill/init
|
||||||
@ -162,9 +162,9 @@ ignored:
|
|||||||
|
|
||||||
# Swagger界面内容配置
|
# Swagger界面内容配置
|
||||||
swagger:
|
swagger:
|
||||||
title: lili API接口文档
|
title: lilishop API接口文档
|
||||||
description: lili Api Documentation
|
description: lilishop Api Documentation
|
||||||
version: 1.0.0
|
version: 4.2.2
|
||||||
termsOfServiceUrl: https://pickmall.cn
|
termsOfServiceUrl: https://pickmall.cn
|
||||||
contact:
|
contact:
|
||||||
name: lili
|
name: lili
|
||||||
|
@ -108,11 +108,6 @@
|
|||||||
<artifactId>knife4j-spring-boot-starter</artifactId>
|
<artifactId>knife4j-spring-boot-starter</artifactId>
|
||||||
<version>${knife4j.version}</version>
|
<version>${knife4j.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
|
||||||
<groupId>com.github.xiaoymin</groupId>
|
|
||||||
<artifactId>swagger-bootstrap-ui</artifactId>
|
|
||||||
<version>${swagger-bootstrap-ui-version}</version>
|
|
||||||
</dependency>
|
|
||||||
<!-- Hutool工具包 -->
|
<!-- Hutool工具包 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.hutool</groupId>
|
<groupId>cn.hutool</groupId>
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
package cn.lili.common.context;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 过滤路径
|
|
||||||
* @author Chopper
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
public class ContextConfiguration implements WebMvcConfigurer {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
|
||||||
registry.addInterceptor(new ThreadContextHolderInterceptorAdapter()).addPathPatterns("/**");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,39 +1,29 @@
|
|||||||
package cn.lili.common.context;
|
package cn.lili.common.context;
|
||||||
|
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户上下文
|
* request / response 获取工具
|
||||||
|
*
|
||||||
* @author paulG
|
* @author paulG
|
||||||
* @since 2020/10/16
|
* @since 2020/10/16
|
||||||
**/
|
**/
|
||||||
public class ThreadContextHolder {
|
public class ThreadContextHolder {
|
||||||
|
|
||||||
private static final ThreadLocal<HttpServletRequest> REQUEST_THREAD_LOCAL_HOLDER = new ThreadLocal<>();
|
|
||||||
private static final ThreadLocal<HttpServletResponse> RESPONSE_THREAD_LOCAL_HOLDER = new ThreadLocal<>();
|
|
||||||
|
|
||||||
public static void remove() {
|
|
||||||
REQUEST_THREAD_LOCAL_HOLDER.remove();
|
|
||||||
RESPONSE_THREAD_LOCAL_HOLDER.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static HttpServletResponse getHttpResponse() {
|
public static HttpServletResponse getHttpResponse() {
|
||||||
|
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
return RESPONSE_THREAD_LOCAL_HOLDER.get();
|
assert servletRequestAttributes != null;
|
||||||
}
|
return servletRequestAttributes.getResponse();
|
||||||
|
|
||||||
public static void setHttpResponse(HttpServletResponse response) {
|
|
||||||
RESPONSE_THREAD_LOCAL_HOLDER.set(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static HttpServletRequest getHttpRequest() {
|
public static HttpServletRequest getHttpRequest() {
|
||||||
return REQUEST_THREAD_LOCAL_HOLDER.get();
|
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
}
|
assert servletRequestAttributes != null;
|
||||||
|
return servletRequestAttributes.getRequest();
|
||||||
public static void setHttpRequest(HttpServletRequest request) {
|
|
||||||
|
|
||||||
REQUEST_THREAD_LOCAL_HOLDER.set(request);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,55 +0,0 @@
|
|||||||
package cn.lili.common.context;
|
|
||||||
|
|
||||||
import org.springframework.lang.Nullable;
|
|
||||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* request response 填充
|
|
||||||
*
|
|
||||||
* @author Chopper
|
|
||||||
* @version v4.0
|
|
||||||
* @since 2020/12/9 10:44
|
|
||||||
*/
|
|
||||||
public class ThreadContextHolderInterceptorAdapter extends HandlerInterceptorAdapter {
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 拦截request和response并放到上下文中
|
|
||||||
*
|
|
||||||
* @param request 请求
|
|
||||||
* @param response 响应
|
|
||||||
* @param handler 处理程序
|
|
||||||
* @return 处理结果
|
|
||||||
* @throws Exception 未知异常
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean preHandle(HttpServletRequest request,
|
|
||||||
HttpServletResponse response, Object handler) throws Exception {
|
|
||||||
|
|
||||||
ThreadContextHolder.setHttpResponse(response);
|
|
||||||
ThreadContextHolder.setHttpRequest(request);
|
|
||||||
|
|
||||||
return super.preHandle(request, response, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 从上下文中移除 request 和response
|
|
||||||
*
|
|
||||||
* @param request 请求
|
|
||||||
* @param response 响应
|
|
||||||
* @param handler 处理程序
|
|
||||||
* @param ex 异常
|
|
||||||
* @throws Exception 完成之前处理异常
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
|
|
||||||
ThreadContextHolder.remove();
|
|
||||||
|
|
||||||
super.afterCompletion(request, response, handler, ex);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
package cn.lili.common.context.interceptor;
|
|
||||||
|
|
||||||
import cn.lili.common.context.ThreadContextHolder;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
import org.springframework.web.servlet.ModelAndView;
|
|
||||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 写入request/response
|
|
||||||
*
|
|
||||||
* @author Chopper
|
|
||||||
* @version v1.0
|
|
||||||
* @since 2020-06-13 13:38
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
@Component
|
|
||||||
public class RequestInterceptorAdapter extends HandlerInterceptorAdapter {
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
|
|
||||||
Object handler) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void postHandle(HttpServletRequest request, HttpServletResponse response,
|
|
||||||
Object handler, ModelAndView modelAndView) throws Exception {
|
|
||||||
|
|
||||||
ThreadContextHolder.setHttpResponse(response);
|
|
||||||
ThreadContextHolder.setHttpRequest(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
|
|
||||||
Object handler, Exception ex) throws Exception {
|
|
||||||
ThreadContextHolder.remove();
|
|
||||||
|
|
||||||
super.afterCompletion(request, response, handler, ex);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
package cn.lili.common.context.interceptor;
|
|
||||||
|
|
||||||
import cn.lili.common.properties.IgnoredUrlsProperties;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
|
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 过滤路径
|
|
||||||
*
|
|
||||||
* @author Chopper
|
|
||||||
*/
|
|
||||||
@Configuration
|
|
||||||
public class UrlConfiguration implements WebMvcConfigurer {
|
|
||||||
@Autowired
|
|
||||||
private IgnoredUrlsProperties ignoredUrlsProperties;
|
|
||||||
@Autowired
|
|
||||||
private RequestInterceptorAdapter requestInterceptorAdapter;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addInterceptors(InterceptorRegistry registry) {
|
|
||||||
//注册拦截器
|
|
||||||
InterceptorRegistration ir = registry.addInterceptor(requestInterceptorAdapter);
|
|
||||||
//配置拦截的路径
|
|
||||||
ir.addPathPatterns("/**");
|
|
||||||
//配置不拦截的路径
|
|
||||||
ir.excludePathPatterns(ignoredUrlsProperties.getUrls());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 开放资源 这里配置swagger可以在前端访问
|
|
||||||
*
|
|
||||||
* @param registry 资源处理
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
||||||
registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
|
|
||||||
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
|
||||||
//解决 SWAGGER 404报错
|
|
||||||
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,8 +1,8 @@
|
|||||||
package cn.lili.modules.connect.entity.enums;
|
package cn.lili.modules.connect.entity.enums;
|
||||||
|
|
||||||
|
import cn.lili.common.utils.StringUtils;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
3
pom.xml
3
pom.xml
@ -21,7 +21,6 @@
|
|||||||
<revision>4.2.2</revision>
|
<revision>4.2.2</revision>
|
||||||
<docker-registry>registry.cn-beijing.aliyuncs.com/lili-images</docker-registry>
|
<docker-registry>registry.cn-beijing.aliyuncs.com/lili-images</docker-registry>
|
||||||
<images-version>1</images-version>
|
<images-version>1</images-version>
|
||||||
<swagger-bootstrap-ui-version>1.9.6</swagger-bootstrap-ui-version>
|
|
||||||
<alipay-sdk-version>4.13.40.ALL</alipay-sdk-version>
|
<alipay-sdk-version>4.13.40.ALL</alipay-sdk-version>
|
||||||
<mysql-connector-version>5.1.48</mysql-connector-version>
|
<mysql-connector-version>5.1.48</mysql-connector-version>
|
||||||
<mybatis-plus-version>3.3.1.tmp</mybatis-plus-version>
|
<mybatis-plus-version>3.3.1.tmp</mybatis-plus-version>
|
||||||
@ -45,7 +44,7 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
<skipTests>true</skipTests>
|
<skipTests>true</skipTests>
|
||||||
<knife4j.version>2.0.8</knife4j.version>
|
<knife4j.version>2.0.9</knife4j.version>
|
||||||
<de.codecentric>2.3.1</de.codecentric>
|
<de.codecentric>2.3.1</de.codecentric>
|
||||||
<userAgentUtils>1.21</userAgentUtils>
|
<userAgentUtils>1.21</userAgentUtils>
|
||||||
<interceptor-api>1.2</interceptor-api>
|
<interceptor-api>1.2</interceptor-api>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user