From 893bb9652e46a18558023a720f01804abdf62d36 Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Wed, 1 Jan 2025 00:24:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(toolbox):=20=E9=87=8D=E5=91=BD=E5=90=8D=20?= =?UTF-8?q?FreemarkerTool=20=E4=B8=BA=20FtlTool=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=20HttpTool=20=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 FreemarkerTool 重命名为 FtlTool,简化类名- 在 HttpTool 中添加了 GET 请求相关方法 - 更新了相关测试类 - 修改了官方文档中的对应部分 --- ...30\346\226\271\346\226\207\346\241\243.md" | 31 +++-- .../{FreemarkerTool.java => FtlTool.java} | 6 +- src/main/java/com/xxl/tool/net/HttpTool.java | 109 ++++++++++++++---- ...eemarkerToolTest.java => FtlToolTest.java} | 10 +- .../com/xxl/tool/test/net/CookieToolTest.java | 12 ++ .../com/xxl/tool/test/net/HttpToolTest.java | 45 +++++++- 6 files changed, 175 insertions(+), 38 deletions(-) rename src/main/java/com/xxl/tool/freemarker/{FreemarkerTool.java => FtlTool.java} (97%) rename src/test/java/com/xxl/tool/test/freemarker/{FreemarkerToolTest.java => FtlToolTest.java} (65%) create mode 100644 src/test/java/com/xxl/tool/test/net/CookieToolTest.java diff --git "a/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" "b/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" index 8abbf3f..c14bab3 100644 --- "a/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" +++ "b/doc/XXL-TOOL\345\256\230\346\226\271\346\226\207\346\241\243.md" @@ -248,7 +248,8 @@ name | 属性/列名称 **使用指南** - a、引入依赖 -添加 xxl-tool 依赖;其次参考仓库pom文件,主动引入如下依赖(默认provided模式,精简不必须依赖)。 +该模块需要主动引入如下关联依赖(默认provided模式,精简不必须依赖),可参考仓库pom获取依赖及版本:https://github.com/xuxueli/xxl-tool/blob/master/pom.xml + ``` org.apache.poi @@ -374,19 +375,35 @@ hexdecimal encode: 一朵美丽的茉莉🌹 hexdecimal decode: 一朵美丽的茉莉🌹 ``` -### 2.7、Core模块 +### 2.7、Freemarker 模块 + +参考单元测试,见目录:com.xxl.tool.test.freemarker.FtlTool -参考单元测试,见目录:com.xxl.tool.test.freemarker.FreemarkerTool ``` // 初始化设置 模板文件目录地址 -FreemarkerTool.init("/Users/admin/Downloads/"); +FtlTool.init("/Users/admin/Downloads/"); // 根据模板文件,生成文本;支持传入变量 -String text = FreemarkerTool.processString("test.ftl", new HashMap<>()); +String text = FtlTool.processString("test.ftl", new HashMap<>()); logger.info(text); ``` -### 2.8、更多 +### 2.8、Http 模块 + +参考单元测试,见目录:com.xxl.tool.test.net.HttpToolTest +``` +// Http Post 请求 +String resp = HttpTool.postBody("http://www.baidu.com/", "hello world"); +String resp = HttpTool.postBody("http://www.baidu.com/", "hello world", 3000); +String resp = HttpTool.postBody("http://www.baidu.com/", "hello world", 3000, headers); + +// Http Get 请求 +String resp = HttpTool.get("http://www.baidu.com/"); +String resp = HttpTool.get("http://www.baidu.com/", 3000); +String resp = HttpTool.get("http://www.baidu.com/", 3000, null); +``` + +### 2.9、更多 略 @@ -426,7 +443,7 @@ logger.info(text); ### 3.6 v1.3.1 Release Notes[2024-11-09] - 1、【强化】已有工具能力完善,包括:StringTool、GsonTool 等; -- 2、【新增】新增多个工具类模块,包括:FreemarkerTool、CookieTool、PageModel、CacheTool、StreamTool 等; +- 2、【新增】新增多个工具类模块,包括:FtlTool、CookieTool、PageModel、CacheTool、StreamTool 等; - 3、【完善】工具类单测完善; - 4、【升级】升级依赖版本,如slf4j、poi、spring、gson…等。 diff --git a/src/main/java/com/xxl/tool/freemarker/FreemarkerTool.java b/src/main/java/com/xxl/tool/freemarker/FtlTool.java similarity index 97% rename from src/main/java/com/xxl/tool/freemarker/FreemarkerTool.java rename to src/main/java/com/xxl/tool/freemarker/FtlTool.java index fe85d9b..a9bd3eb 100644 --- a/src/main/java/com/xxl/tool/freemarker/FreemarkerTool.java +++ b/src/main/java/com/xxl/tool/freemarker/FtlTool.java @@ -19,8 +19,8 @@ * * @author xuxueli 2018-05-02 19:56:00 */ -public class FreemarkerTool { - private static final Logger logger = LoggerFactory.getLogger(FreemarkerTool.class); +public class FtlTool { + private static final Logger logger = LoggerFactory.getLogger(FtlTool.class); /** * freemarker config @@ -54,7 +54,7 @@ public static void init(String templatePath){ * @param freemarkerConfig */ public static void init(Configuration freemarkerConfig){ - FreemarkerTool.freemarkerConfig = freemarkerConfig; + FtlTool.freemarkerConfig = freemarkerConfig; } diff --git a/src/main/java/com/xxl/tool/net/HttpTool.java b/src/main/java/com/xxl/tool/net/HttpTool.java index f928cce..e5dc47d 100644 --- a/src/main/java/com/xxl/tool/net/HttpTool.java +++ b/src/main/java/com/xxl/tool/net/HttpTool.java @@ -54,23 +54,6 @@ public void checkServerTrusted(X509Certificate[] chain, String authType) throws }}; // trust-https end - - // ---------------------- tool ---------------------- - - /** - * post - * - * @param url - * @param requestBody - * @param timeout - * @return - */ - public static String postBody(String url, - String requestBody, - int timeout) { - return postBody(url, requestBody, null, timeout); - } - /** * post * @@ -80,10 +63,11 @@ public static String postBody(String url, * @param timeout * @return */ - public static String postBody(String url, - String requestBody, - Map headers, - int timeout) { + private static String doRequest(String method, + String url, + String requestBody, + int timeout, + Map headers) { HttpURLConnection connection = null; BufferedReader bufferedReader = null; @@ -100,7 +84,7 @@ public static String postBody(String url, } // connection setting - connection.setRequestMethod("POST"); + connection.setRequestMethod(method); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); @@ -168,4 +152,85 @@ public static String postBody(String url, } } + // ---------------------- post ---------------------- + + /** + * post + * + * @param url + * @param requestBody + * @param timeout + * @param headers + * @return + */ + public static String postBody(String url, + String requestBody, + int timeout, + Map headers) { + return doRequest("POST", url, requestBody, timeout, headers); + } + + /** + * post + * + * @param url + * @param requestBody + * @param timeout + * @return + */ + public static String postBody(String url, + String requestBody, + int timeout) { + return doRequest("POST", url, requestBody, timeout, null); + } + + /** + * post + * + * @param url + * @param requestBody + * @return + */ + public static String postBody(String url, String requestBody) { + return doRequest("POST", url, requestBody, 1000, null); + } + + // ---------------------- get ---------------------- + + /** + * get + * + * @param url + * @param timeout + * @param headers + * @return + */ + public static String get(String url, + int timeout, + Map headers) { + return doRequest("POST", url, null, timeout, headers); + } + + /** + * get + * + * @param url + * @param timeout + * @return + */ + public static String get(String url, int timeout) { + return doRequest("POST", url, null, timeout, null); + } + + /** + * get + * + * @param url + * @return + */ + public static String get(String url) { + return doRequest("POST", url, null, 1000, null); + } + + } diff --git a/src/test/java/com/xxl/tool/test/freemarker/FreemarkerToolTest.java b/src/test/java/com/xxl/tool/test/freemarker/FtlToolTest.java similarity index 65% rename from src/test/java/com/xxl/tool/test/freemarker/FreemarkerToolTest.java rename to src/test/java/com/xxl/tool/test/freemarker/FtlToolTest.java index 4a47e92..30d2e9b 100644 --- a/src/test/java/com/xxl/tool/test/freemarker/FreemarkerToolTest.java +++ b/src/test/java/com/xxl/tool/test/freemarker/FtlToolTest.java @@ -1,6 +1,6 @@ package com.xxl.tool.test.freemarker; -import com.xxl.tool.freemarker.FreemarkerTool; +import com.xxl.tool.freemarker.FtlTool; import freemarker.template.TemplateException; import org.junit.jupiter.api.Test; import org.slf4j.Logger; @@ -9,16 +9,16 @@ import java.io.IOException; import java.util.HashMap; -public class FreemarkerToolTest { - private static final Logger logger = LoggerFactory.getLogger(FreemarkerToolTest.class); +public class FtlToolTest { + private static final Logger logger = LoggerFactory.getLogger(FtlToolTest.class); @Test public void test() throws TemplateException, IOException { // init template path - FreemarkerTool.init("/Users/admin/Downloads/"); + FtlTool.init("/Users/admin/Downloads/"); // generate text by template - String text = FreemarkerTool.processString("test.ftl", new HashMap<>()); + String text = FtlTool.processString("test.ftl", new HashMap<>()); logger.info(text); } diff --git a/src/test/java/com/xxl/tool/test/net/CookieToolTest.java b/src/test/java/com/xxl/tool/test/net/CookieToolTest.java new file mode 100644 index 0000000..4d1ee44 --- /dev/null +++ b/src/test/java/com/xxl/tool/test/net/CookieToolTest.java @@ -0,0 +1,12 @@ +package com.xxl.tool.test.net; + +import org.junit.jupiter.api.Test; + + +public class CookieToolTest { + + @Test + public void test() { + } + +} diff --git a/src/test/java/com/xxl/tool/test/net/HttpToolTest.java b/src/test/java/com/xxl/tool/test/net/HttpToolTest.java index edbf592..dabf9b6 100644 --- a/src/test/java/com/xxl/tool/test/net/HttpToolTest.java +++ b/src/test/java/com/xxl/tool/test/net/HttpToolTest.java @@ -5,12 +5,55 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.HashMap; +import java.util.Map; + public class HttpToolTest { private static final Logger logger = LoggerFactory.getLogger(HttpToolTest.class); + @Test + public void test3() { + String resp = HttpTool.postBody("http://www.baidu.com/", "hello world"); + logger.info(resp); + } + + @Test + public void test2() { + String resp = HttpTool.postBody( + "http://www.baidu.com/", + "hello world", + 3000); + logger.info(resp); + } + @Test public void test() { - String resp = HttpTool.postBody("http://www.baidu.com/", "hello world", 3000); + Map headers = new HashMap<>(); + headers.put("Content-Type", "application/json"); + + String resp = HttpTool.postBody( + "http://www.baidu.com/", + "hello world", + 3000, + headers); + logger.info(resp); + } + + @Test + public void test1() { + String resp = HttpTool.get("http://www.baidu.com/"); + logger.info(resp); + } + + @Test + public void test4() { + String resp = HttpTool.get("http://www.baidu.com/", 3000); + logger.info(resp); + } + + @Test + public void test5() { + String resp = HttpTool.get("http://www.baidu.com/", 3000, null); logger.info(resp); }