mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor: 优化部分代码及方法命名,移除 SpringWebUtils 部分重复方法
This commit is contained in:
@@ -30,24 +30,24 @@ import java.util.Objects;
|
||||
* json 构建工具
|
||||
*
|
||||
* @author echo
|
||||
* @since 2025/03/31
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public class JsonBuilder {
|
||||
public class JSONBuilder {
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = SpringUtil.getBean(ObjectMapper.class);
|
||||
private final ObjectNode rootNode;
|
||||
|
||||
private JsonBuilder() {
|
||||
private JSONBuilder() {
|
||||
this.rootNode = OBJECT_MAPPER.createObjectNode();
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始构建
|
||||
*
|
||||
* @return {@link JsonBuilder }
|
||||
* @return {@link JSONBuilder }
|
||||
*/
|
||||
public static JsonBuilder builder() {
|
||||
return new JsonBuilder();
|
||||
public static JSONBuilder builder() {
|
||||
return new JSONBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,9 +55,9 @@ public class JsonBuilder {
|
||||
*
|
||||
* @param key key 值
|
||||
* @param value 值
|
||||
* @return {@link JsonBuilder }
|
||||
* @return {@link JSONBuilder }
|
||||
*/
|
||||
public JsonBuilder add(String key, String value) {
|
||||
public JSONBuilder add(String key, String value) {
|
||||
Objects.requireNonNull(key, "键不能为 null");
|
||||
if (value != null) {
|
||||
rootNode.put(key, value);
|
||||
@@ -70,9 +70,9 @@ public class JsonBuilder {
|
||||
*
|
||||
* @param key key 值
|
||||
* @param value 值
|
||||
* @return {@link JsonBuilder }
|
||||
* @return {@link JSONBuilder }
|
||||
*/
|
||||
public JsonBuilder add(String key, int value) {
|
||||
public JSONBuilder add(String key, int value) {
|
||||
Objects.requireNonNull(key, "键不能为 null");
|
||||
rootNode.put(key, value);
|
||||
return this;
|
||||
@@ -83,9 +83,9 @@ public class JsonBuilder {
|
||||
*
|
||||
* @param key key 值
|
||||
* @param value 值
|
||||
* @return {@link JsonBuilder }
|
||||
* @return {@link JSONBuilder }
|
||||
*/
|
||||
public JsonBuilder add(String key, long value) {
|
||||
public JSONBuilder add(String key, long value) {
|
||||
Objects.requireNonNull(key, "键不能为 null");
|
||||
rootNode.put(key, value);
|
||||
return this;
|
||||
@@ -96,9 +96,9 @@ public class JsonBuilder {
|
||||
*
|
||||
* @param key key 值
|
||||
* @param value 值
|
||||
* @return {@link JsonBuilder }
|
||||
* @return {@link JSONBuilder }
|
||||
*/
|
||||
public JsonBuilder add(String key, boolean value) {
|
||||
public JSONBuilder add(String key, boolean value) {
|
||||
Objects.requireNonNull(key, "键不能为 null");
|
||||
rootNode.put(key, value);
|
||||
return this;
|
||||
@@ -109,9 +109,9 @@ public class JsonBuilder {
|
||||
*
|
||||
* @param key key 值
|
||||
* @param value 值
|
||||
* @return {@link JsonBuilder }
|
||||
* @return {@link JSONBuilder }
|
||||
*/
|
||||
public JsonBuilder add(String key, double value) {
|
||||
public JSONBuilder add(String key, double value) {
|
||||
Objects.requireNonNull(key, "键不能为 null");
|
||||
rootNode.put(key, value);
|
||||
return this;
|
||||
@@ -122,9 +122,9 @@ public class JsonBuilder {
|
||||
*
|
||||
* @param key key 值
|
||||
* @param value 值
|
||||
* @return {@link JsonBuilder }
|
||||
* @return {@link JSONBuilder }
|
||||
*/
|
||||
public JsonBuilder add(String key, JsonNode value) {
|
||||
public JSONBuilder add(String key, JsonNode value) {
|
||||
Objects.requireNonNull(key, "键不能为 null");
|
||||
if (value != null) {
|
||||
rootNode.set(key, value);
|
||||
@@ -137,9 +137,9 @@ public class JsonBuilder {
|
||||
*
|
||||
* @param key key 值
|
||||
* @param value 值
|
||||
* @return {@link JsonBuilder }
|
||||
* @return {@link JSONBuilder }
|
||||
*/
|
||||
public JsonBuilder add(String key, Object value) {
|
||||
public JSONBuilder add(String key, Object value) {
|
||||
Objects.requireNonNull(key, "键不能为 null");
|
||||
if (value != null) {
|
||||
rootNode.set(key, OBJECT_MAPPER.valueToTree(value));
|
||||
@@ -152,9 +152,9 @@ public class JsonBuilder {
|
||||
*
|
||||
* @param key key 值
|
||||
* @param list list 参数
|
||||
* @return {@link JsonBuilder }
|
||||
* @return {@link JSONBuilder }
|
||||
*/
|
||||
public JsonBuilder add(String key, List<?> list) {
|
||||
public JSONBuilder add(String key, List<?> list) {
|
||||
Objects.requireNonNull(key, "键不能为 null");
|
||||
if (list != null) {
|
||||
ArrayNode arrayNode = OBJECT_MAPPER.createArrayNode();
|
||||
@@ -171,9 +171,9 @@ public class JsonBuilder {
|
||||
*
|
||||
* @param key key 值
|
||||
* @param map map 参数
|
||||
* @return {@link JsonBuilder }
|
||||
* @return {@link JSONBuilder }
|
||||
*/
|
||||
public JsonBuilder add(String key, Map<?, ?> map) {
|
||||
public JSONBuilder add(String key, Map<?, ?> map) {
|
||||
Objects.requireNonNull(key, "键不能为 null");
|
||||
if (map != null) {
|
||||
ObjectNode objectNode = OBJECT_MAPPER.valueToTree(map);
|
@@ -33,13 +33,11 @@ import java.util.Map;
|
||||
* json 工具
|
||||
*
|
||||
* @author echo
|
||||
* @since 2025/03/31
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public class JSONUtil {
|
||||
/**
|
||||
* 私有构造函数,防止实例化。
|
||||
*/
|
||||
private JSONUtil() {
|
||||
public class JSONUtils {
|
||||
|
||||
private JSONUtils() {
|
||||
}
|
||||
|
||||
/**
|
@@ -166,14 +166,14 @@ public abstract class AbstractLogHandler implements LogHandler {
|
||||
AccessLogProperties properties = accessLogContext.getProperties().getAccessLog();
|
||||
// 是否需要打印 规则: 是否打印开关 或 放行路径
|
||||
if (!properties.isEnabled() || AccessLogUtils.exclusionPath(accessLogContext.getProperties(), ServletUtils
|
||||
.getReqPath())) {
|
||||
.getRequestPath())) {
|
||||
return;
|
||||
}
|
||||
// 构建上下文
|
||||
logContextThread.set(accessLogContext);
|
||||
String param = AccessLogUtils.getParam(properties);
|
||||
log.info(param != null ? "[Start] [{}] {} param: {}" : "[Start] [{}] {}", ServletUtils
|
||||
.getReqMethod(), ServletUtils.getReqPath(), param);
|
||||
.getRequestMethod(), ServletUtils.getRequestPath(), param);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -184,8 +184,8 @@ public abstract class AbstractLogHandler implements LogHandler {
|
||||
}
|
||||
try {
|
||||
Duration timeTaken = Duration.between(logContext.getStartTime(), accessLogContext.getEndTime());
|
||||
log.info("[End] [{}] {} {} {}ms", ServletUtils.getReqMethod(), ServletUtils.getReqPath(), ServletUtils
|
||||
.getRespStatus(), timeTaken.toMillis());
|
||||
log.info("[End] [{}] {} {} {}ms", ServletUtils.getRequestMethod(), ServletUtils
|
||||
.getRequestPath(), ServletUtils.getResponseStatus(), timeTaken.toMillis());
|
||||
} finally {
|
||||
logContextThread.remove();
|
||||
}
|
||||
|
@@ -81,14 +81,14 @@ public class LogRequest {
|
||||
private String os;
|
||||
|
||||
public LogRequest(Set<Include> includes) {
|
||||
this.method = ServletUtils.getReqMethod();
|
||||
this.url = ServletUtils.getReqUrl();
|
||||
this.ip = ServletUtils.getReqIp();
|
||||
this.headers = (includes.contains(Include.REQUEST_HEADERS)) ? ServletUtils.getReqHeaders() : null;
|
||||
this.method = ServletUtils.getRequestMethod();
|
||||
this.url = ServletUtils.getRequestUrl();
|
||||
this.ip = ServletUtils.getRequestIp();
|
||||
this.headers = (includes.contains(Include.REQUEST_HEADERS)) ? ServletUtils.getRequestHeaders() : null;
|
||||
if (includes.contains(Include.REQUEST_BODY)) {
|
||||
this.body = ServletUtils.getReqBody();
|
||||
this.body = ServletUtils.getRequestBody();
|
||||
} else if (includes.contains(Include.REQUEST_PARAM)) {
|
||||
this.param = ServletUtils.getReqParam();
|
||||
this.param = ServletUtils.getRequestParams();
|
||||
}
|
||||
this.address = (includes.contains(Include.IP_ADDRESS))
|
||||
? ExceptionUtils.exToNull(() -> IpUtils.getIpv4Address(this.ip))
|
||||
|
@@ -51,12 +51,12 @@ public class LogResponse {
|
||||
private Map<String, Object> param;
|
||||
|
||||
public LogResponse(Set<Include> includes) {
|
||||
this.status = ServletUtils.getRespStatus();
|
||||
this.headers = (includes.contains(Include.RESPONSE_HEADERS)) ? ServletUtils.getRespHeaders() : null;
|
||||
this.status = ServletUtils.getResponseStatus();
|
||||
this.headers = (includes.contains(Include.RESPONSE_HEADERS)) ? ServletUtils.getResponseHeaders() : null;
|
||||
if (includes.contains(Include.RESPONSE_BODY)) {
|
||||
this.body = ServletUtils.getRespBody();
|
||||
this.body = ServletUtils.getResponseBody();
|
||||
} else if (includes.contains(Include.RESPONSE_PARAM)) {
|
||||
this.param = ServletUtils.getRespParam();
|
||||
this.param = ServletUtils.getResponseParams();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -16,16 +16,16 @@
|
||||
|
||||
package top.continew.starter.log.util;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import top.continew.starter.json.jackson.util.JSONUtil;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import top.continew.starter.json.jackson.util.JSONUtils;
|
||||
import top.continew.starter.log.model.AccessLogProperties;
|
||||
import top.continew.starter.log.model.LogProperties;
|
||||
import top.continew.starter.web.util.ServletUtils;
|
||||
import top.continew.starter.web.util.SpringWebUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -37,6 +37,9 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
public class AccessLogUtils {
|
||||
|
||||
private AccessLogUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 资源路径 - doc 路径
|
||||
*/
|
||||
@@ -58,7 +61,7 @@ public class AccessLogUtils {
|
||||
// 参数为空返回空
|
||||
Object params;
|
||||
try {
|
||||
params = ServletUtils.getAccessLogReqParam();
|
||||
params = getAccessLogReqParam();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
@@ -76,7 +79,7 @@ public class AccessLogUtils {
|
||||
params = processTruncateLongParams(params, properties.getLongParamThreshold(), properties
|
||||
.getLongParamMaxLength(), properties.getLongParamSuffix());
|
||||
}
|
||||
return JSONUtil.toJsonStr(params);
|
||||
return JSONUtils.toJsonStr(params);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,7 +149,7 @@ public class AccessLogUtils {
|
||||
return truncateLongParams((Map<String, Object>)params, threshold, maxLength, suffix);
|
||||
} else if (params instanceof List) {
|
||||
return ((List<?>)params).stream()
|
||||
.filter(item -> item instanceof Map)
|
||||
.filter(Map.class::isInstance)
|
||||
.map(item -> truncateLongParams((Map<String, Object>)item, threshold, maxLength, suffix))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
@@ -182,6 +185,25 @@ public class AccessLogUtils {
|
||||
return truncatedParams;
|
||||
}
|
||||
|
||||
private AccessLogUtils() {
|
||||
/**
|
||||
* 获取访问日志请求参数
|
||||
*
|
||||
* @return {@link Object }
|
||||
*/
|
||||
private static Object getAccessLogReqParam() {
|
||||
String body = ServletUtils.getRequestBody();
|
||||
if (CharSequenceUtil.isNotBlank(body) && JSONUtils.isTypeJSON(body)) {
|
||||
try {
|
||||
JsonNode jsonNode = JSONUtils.getObjectMapper().readTree(body);
|
||||
if (jsonNode.isArray()) {
|
||||
return JSONUtils.toBean(body, List.class);
|
||||
} else {
|
||||
return JSONUtils.toBean(body, Map.class);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return Collections.unmodifiableMap(ServletUtils.getRequestParams());
|
||||
}
|
||||
}
|
||||
|
@@ -19,7 +19,6 @@ package top.continew.starter.ratelimiter.aop;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.extra.servlet.JakartaServletUtil;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
@@ -36,7 +35,7 @@ import top.continew.starter.ratelimiter.autoconfigure.RateLimiterProperties;
|
||||
import top.continew.starter.ratelimiter.generator.RateLimiterNameGenerator;
|
||||
import top.continew.starter.ratelimiter.enums.LimitType;
|
||||
import top.continew.starter.ratelimiter.exception.RateLimiterException;
|
||||
import top.continew.starter.web.util.SpringWebUtils;
|
||||
import top.continew.starter.web.util.ServletUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.time.Duration;
|
||||
@@ -170,7 +169,7 @@ public class RateLimiterAspect {
|
||||
}
|
||||
// 获取后缀
|
||||
String suffix = switch (rateLimiter.type()) {
|
||||
case IP -> JakartaServletUtil.getClientIP(SpringWebUtils.getRequest());
|
||||
case IP -> ServletUtils.getRequestIp();
|
||||
case CLUSTER -> redissonClient.getId();
|
||||
default -> StringConstants.EMPTY;
|
||||
};
|
||||
|
@@ -21,7 +21,6 @@ import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.extra.servlet.JakartaServletUtil;
|
||||
import cn.hutool.http.useragent.UserAgent;
|
||||
import cn.hutool.http.useragent.UserAgentUtil;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
@@ -30,7 +29,7 @@ import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.util.UriUtils;
|
||||
import top.continew.starter.core.constant.StringConstants;
|
||||
import top.continew.starter.json.jackson.util.JSONUtil;
|
||||
import top.continew.starter.json.jackson.util.JSONUtils;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
@@ -41,6 +40,7 @@ import java.util.*;
|
||||
* Servlet 工具类
|
||||
*
|
||||
* @author Charles7c
|
||||
* @author echo
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ServletUtils extends JakartaServletUtil {
|
||||
@@ -48,20 +48,6 @@ public class ServletUtils extends JakartaServletUtil {
|
||||
private ServletUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求属性
|
||||
*
|
||||
* @return {@link ServletRequestAttributes }
|
||||
*/
|
||||
public static ServletRequestAttributes getRequestAttributes() {
|
||||
try {
|
||||
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
|
||||
return (ServletRequestAttributes)attributes;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取浏览器及其版本信息
|
||||
*
|
||||
@@ -110,46 +96,25 @@ public class ServletUtils extends JakartaServletUtil {
|
||||
return userAgent.getOs().getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 http request
|
||||
*
|
||||
* @return HttpServletRequest
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
ServletRequestAttributes attributes = getRequestAttributes();
|
||||
if (attributes == null) {
|
||||
return null;
|
||||
}
|
||||
return attributes.getRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求方法
|
||||
*
|
||||
* @return {@link String }
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static String getReqMethod() {
|
||||
public static String getRequestMethod() {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? request.getMethod() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取session
|
||||
*
|
||||
* @return HttpSession
|
||||
*/
|
||||
public static HttpSession getSession() {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? request.getSession() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 请求 字符串参数
|
||||
* 获取请求参数
|
||||
*
|
||||
* @param name 参数名
|
||||
* @return {@link String }
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static String getReqParameter(String name) {
|
||||
public static String getRequestParameter(String name) {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? request.getParameter(name) : null;
|
||||
}
|
||||
@@ -158,8 +123,9 @@ public class ServletUtils extends JakartaServletUtil {
|
||||
* 获取请求 Ip
|
||||
*
|
||||
* @return {@link String }
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static String getReqIp() {
|
||||
public static String getRequestIp() {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? getClientIP(request) : null;
|
||||
}
|
||||
@@ -168,19 +134,21 @@ public class ServletUtils extends JakartaServletUtil {
|
||||
* 获取请求头信息
|
||||
*
|
||||
* @return {@link Map }<{@link String }, {@link String }>
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static Map<String, String> getReqHeaders() {
|
||||
public static Map<String, String> getRequestHeaders() {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? getHeaderMap(request) : Collections.emptyMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求url 包含 query 参数
|
||||
* <p>http://localhost:8000/system/user?page=1&size=10</p>
|
||||
* 获取请求 URL(包含 query 参数)
|
||||
* <p>{@code http://localhost:8000/system/user?page=1&size=10}</p>
|
||||
*
|
||||
* @return {@link URI }
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static URI getReqUrl() {
|
||||
public static URI getRequestUrl() {
|
||||
HttpServletRequest request = getRequest();
|
||||
if (request == null) {
|
||||
return null;
|
||||
@@ -203,8 +171,9 @@ public class ServletUtils extends JakartaServletUtil {
|
||||
* 获取请求路径
|
||||
*
|
||||
* @return {@link URI }
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static String getReqPath() {
|
||||
public static String getRequestPath() {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? request.getRequestURI() : null;
|
||||
}
|
||||
@@ -213,12 +182,13 @@ public class ServletUtils extends JakartaServletUtil {
|
||||
* 获取请求 body 参数
|
||||
*
|
||||
* @return {@link String }
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static String getReqBody() {
|
||||
public static String getRequestBody() {
|
||||
HttpServletRequest request = getRequest();
|
||||
if (request instanceof RepeatReadRequestWrapper wrapper && !wrapper.isMultipartContent(request)) {
|
||||
String body = JakartaServletUtil.getBody(request);
|
||||
return JSONUtil.isTypeJSON(body) ? body : null;
|
||||
return JSONUtils.isTypeJSON(body) ? body : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -227,55 +197,22 @@ public class ServletUtils extends JakartaServletUtil {
|
||||
* 获取请求参数
|
||||
*
|
||||
* @return {@link Map }<{@link String }, {@link Object }>
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static Map<String, Object> getReqParam() {
|
||||
String body = getReqBody();
|
||||
return CharSequenceUtil.isNotBlank(body) && JSONUtil.isTypeJSON(body)
|
||||
? JSONUtil.toBean(body, Map.class)
|
||||
public static Map<String, Object> getRequestParams() {
|
||||
String body = getRequestBody();
|
||||
return CharSequenceUtil.isNotBlank(body) && JSONUtils.isTypeJSON(body)
|
||||
? JSONUtils.toBean(body, Map.class)
|
||||
: Collections.unmodifiableMap(JakartaServletUtil.getParamMap(Objects.requireNonNull(getRequest())));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取访问日志请求参数
|
||||
*
|
||||
* @return {@link Object }
|
||||
*/
|
||||
public static Object getAccessLogReqParam() {
|
||||
String body = getReqBody();
|
||||
if (CharSequenceUtil.isNotBlank(body) && JSONUtil.isTypeJSON(body)) {
|
||||
try {
|
||||
JsonNode jsonNode = JSONUtil.getObjectMapper().readTree(body);
|
||||
if (jsonNode.isArray()) {
|
||||
return JSONUtil.toBean(body, List.class);
|
||||
} else {
|
||||
return JSONUtil.toBean(body, Map.class);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return Collections.unmodifiableMap(JakartaServletUtil.getParamMap(Objects.requireNonNull(getRequest())));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 http response
|
||||
*
|
||||
* @return HttpServletResponse
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
ServletRequestAttributes attributes = getRequestAttributes();
|
||||
if (attributes == null) {
|
||||
return null;
|
||||
}
|
||||
return attributes.getResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取响应状态
|
||||
*
|
||||
* @return int
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static int getRespStatus() {
|
||||
public static int getResponseStatus() {
|
||||
HttpServletResponse response = getResponse();
|
||||
return response != null ? response.getStatus() : -1;
|
||||
}
|
||||
@@ -284,8 +221,9 @@ public class ServletUtils extends JakartaServletUtil {
|
||||
* 获取响应所有的头(header)信息
|
||||
*
|
||||
* @return header值
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static Map<String, String> getRespHeaders() {
|
||||
public static Map<String, String> getResponseHeaders() {
|
||||
HttpServletResponse response = getResponse();
|
||||
if (response == null) {
|
||||
return Collections.emptyMap();
|
||||
@@ -302,19 +240,82 @@ public class ServletUtils extends JakartaServletUtil {
|
||||
* 获取响应 body 参数
|
||||
*
|
||||
* @return {@link String }
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static String getRespBody() {
|
||||
public static String getResponseBody() {
|
||||
HttpServletResponse response = getResponse();
|
||||
if (response instanceof RepeatReadResponseWrapper wrapper && !wrapper.isStreamingResponse()) {
|
||||
String body = wrapper.getResponseContent();
|
||||
return JSONUtil.isTypeJSON(body) ? body : null;
|
||||
return JSONUtils.isTypeJSON(body) ? body : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Map<String, Object> getRespParam() {
|
||||
String body = getRespBody();
|
||||
return CharSequenceUtil.isNotBlank(body) && JSONUtil.isTypeJSON(body) ? JSONUtil.toBean(body, Map.class) : null;
|
||||
/**
|
||||
* 获取响应参数
|
||||
*
|
||||
* @return {@link Map }<{@link String }, {@link Object }>
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static Map<String, Object> getResponseParams() {
|
||||
String body = getResponseBody();
|
||||
return CharSequenceUtil.isNotBlank(body) && JSONUtils.isTypeJSON(body)
|
||||
? JSONUtils.toBean(body, Map.class)
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 HTTP Session
|
||||
*
|
||||
* @return HttpSession
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static HttpSession getSession() {
|
||||
HttpServletRequest request = getRequest();
|
||||
return request != null ? request.getSession() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 HTTP Request
|
||||
*
|
||||
* @return HttpServletRequest
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
ServletRequestAttributes attributes = getRequestAttributes();
|
||||
if (attributes == null) {
|
||||
return null;
|
||||
}
|
||||
return attributes.getRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 HTTP Response
|
||||
*
|
||||
* @return HttpServletResponse
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
ServletRequestAttributes attributes = getRequestAttributes();
|
||||
if (attributes == null) {
|
||||
return null;
|
||||
}
|
||||
return attributes.getResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求属性
|
||||
*
|
||||
* @return {@link ServletRequestAttributes }
|
||||
* @since 2.11.0
|
||||
*/
|
||||
public static ServletRequestAttributes getRequestAttributes() {
|
||||
try {
|
||||
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
|
||||
return (ServletRequestAttributes)attributes;
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -20,13 +20,9 @@ import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import jakarta.servlet.ServletContext;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.http.server.PathContainer;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
@@ -38,7 +34,6 @@ import top.continew.starter.core.constant.StringConstants;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Spring Web 工具类
|
||||
@@ -51,24 +46,6 @@ public class SpringWebUtils {
|
||||
private SpringWebUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取请求对象
|
||||
*
|
||||
* @return 请求对象
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
return getServletRequestAttributes().getRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取响应对象
|
||||
*
|
||||
* @return 响应对象
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
return getServletRequestAttributes().getResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 路径是否匹配
|
||||
*
|
||||
@@ -157,8 +134,4 @@ public class SpringWebUtils {
|
||||
.getUrlMap();
|
||||
ReflectUtil.<Void>invoke(resourceHandlerMapping, "registerHandlers", additionalUrlMap);
|
||||
}
|
||||
|
||||
private static ServletRequestAttributes getServletRequestAttributes() {
|
||||
return (ServletRequestAttributes)Objects.requireNonNull(RequestContextHolder.getRequestAttributes());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user