Skip to content

Commit

Permalink
feat(toolbox): 重命名 FreemarkerTool 为 FtlTool并添加 HttpTool 功能
Browse files Browse the repository at this point in the history
- 将 FreemarkerTool 重命名为 FtlTool,简化类名- 在 HttpTool 中添加了 GET 请求相关方法
- 更新了相关测试类
- 修改了官方文档中的对应部分
  • Loading branch information
xuxueli committed Dec 31, 2024
1 parent 84bde6f commit 893bb96
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 38 deletions.
31 changes: 24 additions & 7 deletions doc/XXL-TOOL官方文档.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ name | 属性/列名称
**使用指南**
- a、引入依赖

添加 xxl-tool 依赖;其次参考仓库pom文件,主动引入如下依赖(默认provided模式,精简不必须依赖)。
该模块需要主动引入如下关联依赖(默认provided模式,精简不必须依赖),可参考仓库pom获取依赖及版本:https://github.com/xuxueli/xxl-tool/blob/master/pom.xml

```
<dependency>
<groupId>org.apache.poi</groupId>
Expand Down Expand Up @@ -374,19 +375,35 @@ hexdecimal encode: 一朵美丽的茉莉&#x1f339;
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、更多


Expand Down Expand Up @@ -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…等。

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -54,7 +54,7 @@ public static void init(String templatePath){
* @param freemarkerConfig
*/
public static void init(Configuration freemarkerConfig){
FreemarkerTool.freemarkerConfig = freemarkerConfig;
FtlTool.freemarkerConfig = freemarkerConfig;
}


Expand Down
109 changes: 87 additions & 22 deletions src/main/java/com/xxl/tool/net/HttpTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -80,10 +63,11 @@ public static String postBody(String url,
* @param timeout
* @return
*/
public static String postBody(String url,
String requestBody,
Map<String, String> headers,
int timeout) {
private static String doRequest(String method,
String url,
String requestBody,
int timeout,
Map<String, String> headers) {

HttpURLConnection connection = null;
BufferedReader bufferedReader = null;
Expand All @@ -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);
Expand Down Expand Up @@ -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<String, String> 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<String, String> 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);
}


}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/xxl/tool/test/net/CookieToolTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.xxl.tool.test.net;

import org.junit.jupiter.api.Test;


public class CookieToolTest {

@Test
public void test() {
}

}
45 changes: 44 additions & 1 deletion src/test/java/com/xxl/tool/test/net/HttpToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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);
}

Expand Down

0 comments on commit 893bb96

Please sign in to comment.