From f2ba10beae9d17fad7fa260924ede5cd8ca0cbe4 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Tue, 1 Apr 2025 22:08:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81=E5=8F=8A=E6=96=B9=E6=B3=95=E5=91=BD?= =?UTF-8?q?=E5=90=8D=EF=BC=8C=E7=A7=BB=E9=99=A4=20SpringWebUtils=20?= =?UTF-8?q?=E9=83=A8=E5=88=86=E9=87=8D=E5=A4=8D=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{JsonBuilder.java => JSONBuilder.java} | 48 ++--- .../util/{JSONUtil.java => JSONUtils.java} | 10 +- .../log/handler/AbstractLogHandler.java | 8 +- .../starter/log/model/LogRequest.java | 12 +- .../starter/log/model/LogResponse.java | 8 +- .../starter/log/util/AccessLogUtils.java | 38 +++- .../ratelimiter/aop/RateLimiterAspect.java | 5 +- .../starter/web/util/ServletUtils.java | 193 +++++++++--------- .../starter/web/util/SpringWebUtils.java | 27 --- 9 files changed, 171 insertions(+), 178 deletions(-) rename continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/{JsonBuilder.java => JSONBuilder.java} (80%) rename continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/{JSONUtil.java => JSONUtils.java} (98%) diff --git a/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/JsonBuilder.java b/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/JSONBuilder.java similarity index 80% rename from continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/JsonBuilder.java rename to continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/JSONBuilder.java index 90065ac3..9cab7b0d 100644 --- a/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/JsonBuilder.java +++ b/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/JSONBuilder.java @@ -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); diff --git a/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/JSONUtil.java b/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/JSONUtils.java similarity index 98% rename from continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/JSONUtil.java rename to continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/JSONUtils.java index f1d860b6..b5903997 100644 --- a/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/JSONUtil.java +++ b/continew-starter-json/continew-starter-json-jackson/src/main/java/top/continew/starter/json/jackson/util/JSONUtils.java @@ -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() { } /** diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/AbstractLogHandler.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/AbstractLogHandler.java index af8598e0..7d64aad9 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/AbstractLogHandler.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/AbstractLogHandler.java @@ -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(); } diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogRequest.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogRequest.java index cb02675d..1b5ae1df 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogRequest.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogRequest.java @@ -81,14 +81,14 @@ public class LogRequest { private String os; public LogRequest(Set 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)) diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogResponse.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogResponse.java index 4b92076b..3bd7bbc1 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogResponse.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogResponse.java @@ -51,12 +51,12 @@ public class LogResponse { private Map param; public LogResponse(Set 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(); } } diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java index 48872fb7..b57dfa49 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java @@ -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)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)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()); } } diff --git a/continew-starter-ratelimiter/src/main/java/top/continew/starter/ratelimiter/aop/RateLimiterAspect.java b/continew-starter-ratelimiter/src/main/java/top/continew/starter/ratelimiter/aop/RateLimiterAspect.java index b41918df..66ac5a60 100644 --- a/continew-starter-ratelimiter/src/main/java/top/continew/starter/ratelimiter/aop/RateLimiterAspect.java +++ b/continew-starter-ratelimiter/src/main/java/top/continew/starter/ratelimiter/aop/RateLimiterAspect.java @@ -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; }; diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/util/ServletUtils.java b/continew-starter-web/src/main/java/top/continew/starter/web/util/ServletUtils.java index 9d30ae60..e49770b0 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/util/ServletUtils.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/util/ServletUtils.java @@ -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 getReqHeaders() { + public static Map getRequestHeaders() { HttpServletRequest request = getRequest(); return request != null ? getHeaderMap(request) : Collections.emptyMap(); } /** - * 获取请求url 包含 query 参数 - *

http://localhost:8000/system/user?page=1&size=10

+ * 获取请求 URL(包含 query 参数) + *

{@code http://localhost:8000/system/user?page=1&size=10}

* * @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 getReqParam() { - String body = getReqBody(); - return CharSequenceUtil.isNotBlank(body) && JSONUtil.isTypeJSON(body) - ? JSONUtil.toBean(body, Map.class) + public static Map 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 getRespHeaders() { + public static Map 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 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 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; + } } /** diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/util/SpringWebUtils.java b/continew-starter-web/src/main/java/top/continew/starter/web/util/SpringWebUtils.java index adaffea8..65f225ff 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/util/SpringWebUtils.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/util/SpringWebUtils.java @@ -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.invoke(resourceHandlerMapping, "registerHandlers", additionalUrlMap); } - - private static ServletRequestAttributes getServletRequestAttributes() { - return (ServletRequestAttributes)Objects.requireNonNull(RequestContextHolder.getRequestAttributes()); - } }