From 67edb0828dee355ff46d055935a76a42a5a6ebd8 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Thu, 17 Jul 2025 20:56:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20ServletUtils=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=BA=94=20JSON=20=E6=95=B0=E6=8D=AE=E7=BB=99=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/core/util/ServletUtils.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/util/ServletUtils.java b/continew-starter-core/src/main/java/top/continew/starter/core/util/ServletUtils.java index 8fcbb1b0..94983646 100644 --- a/continew-starter-core/src/main/java/top/continew/starter/core/util/ServletUtils.java +++ b/continew-starter-core/src/main/java/top/continew/starter/core/util/ServletUtils.java @@ -25,6 +25,7 @@ import cn.hutool.json.JSONUtil; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; +import org.springframework.http.MediaType; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; @@ -36,7 +37,10 @@ import top.continew.starter.core.wrapper.RepeatReadResponseWrapper; import java.net.URI; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; -import java.util.*; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.Objects; /** * Servlet 工具类 @@ -334,6 +338,18 @@ public class ServletUtils extends JakartaServletUtil { } } + /** + * 响应 JSON 数据给客户端 + * + * @param response 响应对象 + * @param data 响应数据 + * @since 2.13.1 + * @see #write(HttpServletResponse, String, String) + */ + public static void writeJSON(HttpServletResponse response, Object data) { + write(response, JSONUtil.toJsonStr(data), MediaType.APPLICATION_JSON_VALUE); + } + /** * 追加查询字符串 *