Skip to content

Commit e90cc0b

Browse files
authored
Merge pull request #350 from mfish-qf/develop
chore: okHttp请求优化、代码生成增加表字段
2 parents f9ac301 + 827a5a0 commit e90cc0b

File tree

4 files changed

+39
-32
lines changed

4 files changed

+39
-32
lines changed

mf-api/mf-sys-api/src/main/java/cn/com/mfish/sys/api/entity/FieldInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
public class FieldInfo implements Serializable {
2121
@Schema(description = "字段名称")
2222
private String fieldName;
23+
@Schema(description = "数据库字段名称")
24+
private String tableFieldName;
2325
@Schema(description = "是否主键 true是 false否")
2426
private Boolean isPrimary = false;
2527
@Schema(description = "JAVA字段类型")

mf-common/mf-common-code/src/main/java/cn/com/mfish/common/code/common/FreemarkerUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public List<CodeVo> getCode(ReqCode reqCode) {
161161
Optional<SearchInfo> searchInfo = codeInfo.getSearchList().stream().filter((search) ->
162162
search.getFieldInfo().getFieldName().equals(finalFieldName) && search.getComponent() != null && search.getComponent().size() == 2)
163163
.findFirst();
164+
fieldInfo.setTableFieldName(fieldName);
164165
fieldInfo.setFieldName(StringUtils.toCamelCase(fieldName));
165166
FieldExpand fieldExpand = new FieldExpand().setFieldInfo(fieldInfo);
166167
//设置字典组件列表

mf-common/mf-common-core/src/main/java/cn/com/mfish/common/core/utils/http/OkHttpUtils.java

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import java.security.KeyStore;
1717
import java.security.SecureRandom;
1818
import java.security.cert.X509Certificate;
19-
import java.text.MessageFormat;
2019
import java.util.Arrays;
2120
import java.util.List;
2221
import java.util.Map;
@@ -61,8 +60,8 @@ public static Result<String> get(String url, Map<String, String> headers) throws
6160
* <p>
6261
* 此方法构造一个带有参数的URL并执行GET请求如果参数为空,则执行不带参数的GET请求
6362
*
64-
* @param url 请求的URL
65-
* @param params 请求的参数,格式为键值对
63+
* @param url 请求的URL
64+
* @param params 请求的参数,格式为键值对
6665
* @param headers 请求的头信息,格式为键值对
6766
* @param timeOut 请求的超时时间
6867
* @return 返回请求的结果
@@ -92,11 +91,11 @@ public static Result<String> get(String url, Map<String, String> headers, TimeOu
9291
return buildResult(okHttpClient, request);
9392
}
9493

95-
public static Result<String> postForm(String url, String params) throws IOException {
94+
public static <T> Result<String> postForm(String url, T params) throws IOException {
9695
return postForm(url, params, null);
9796
}
9897

99-
public static Result<String> postForm(String url, String params, Map<String, String> headers) throws IOException {
98+
public static <T> Result<String> postForm(String url, T params, Map<String, String> headers) throws IOException {
10099
return postForm(url, params, headers, null);
101100
}
102101

@@ -106,22 +105,22 @@ public static Result<String> postForm(String url, String params, Map<String, Str
106105
* 本方法专注于处理Form类型的POST请求,通过提供的URL、请求参数、自定义请求头和超时设置,
107106
* 来执行网络请求,并返回请求的结果。使用较低级别的HTTP API来实现自定义请求的需求。
108107
*
109-
* @param url 请求的URL,指定了资源的位置
110-
* @param params 请求参数,以字符串形式提交,通常为键值对的形式
108+
* @param url 请求的URL,指定了资源的位置
109+
* @param params 请求参数,以字符串形式提交,通常为键值对的形式
111110
* @param headers 请求头信息,包含HTTP请求头部字段和对应的值,可为空
112111
* @param timeOut 请求的超时设置,决定了请求各阶段的最大等待时间
113112
* @return 返回请求的结果,包括状态码、响应头和响应体等信息
114113
* @throws IOException 如果请求过程中发生I/O错误,如网络中断等
115114
*/
116-
public static Result<String> postForm(String url, String params, Map<String, String> headers, TimeOut timeOut) throws IOException {
115+
public static <T> Result<String> postForm(String url, T params, Map<String, String> headers, TimeOut timeOut) throws IOException {
117116
return post(url, params, headers, timeOut, Form_TYPE);
118117
}
119118

120-
public static Result<String> postJson(String url, String params) throws IOException {
119+
public static <T> Result<String> postJson(String url, T params) throws IOException {
121120
return postJson(url, params, null);
122121
}
123122

124-
public static Result<String> postJson(String url, String params, Map<String, String> headers) throws IOException {
123+
public static <T> Result<String> postJson(String url, T params, Map<String, String> headers) throws IOException {
125124
return postJson(url, params, headers, null);
126125
}
127126

@@ -130,13 +129,13 @@ public static Result<String> postJson(String url, String params, Map<String, Str
130129
* 此方法用于发送带有JSON数据的HTTP POST请求到指定的URL
131130
* 它允许自定义请求头和超时设置,以满足不同的网络请求需求
132131
*
133-
* @param url 请求的URL地址,指定资源位置
134-
* @param params 请求的JSON参数,以字符串形式表示
132+
* @param url 请求的URL地址,指定资源位置
133+
* @param params 请求的JSON参数,以字符串形式表示
135134
* @param headers 请求头信息,包含请求的附加信息,如认证信息、接受类型等
136135
* @param timeOut 连接和读取超时设置,用于控制网络请求的响应时间
137136
* @return 返回包含响应结果的Result对象,响应体以字符串形式存储
138137
*/
139-
public static Result<String> postJson(String url, String params, Map<String, String> headers, TimeOut timeOut) throws IOException {
138+
public static <T> Result<String> postJson(String url, T params, Map<String, String> headers, TimeOut timeOut) throws IOException {
140139
return post(url, params, headers, timeOut, JSON_TYPE);
141140
}
142141

@@ -145,17 +144,23 @@ public static Result<String> postJson(String url, String params, Map<String, Str
145144
* 该方法负责发起HTTP POST请求,根据提供的参数构建请求并发送
146145
* 主要用于需要向服务器提交数据的场景
147146
*
148-
* @param url 请求的URL,表示服务器的地址
149-
* @param params 请求的参数,通常为JSON字符串或其他格式的数据
150-
* @param headers 请求头部,包含一些附加信息如认证信息、内容类型等
151-
* @param timeOut 请求的超时时间设置
147+
* @param url 请求的URL,表示服务器的地址
148+
* @param params 请求的参数,通常为JSON字符串或其他格式的数据
149+
* @param headers 请求头部,包含一些附加信息如认证信息、内容类型等
150+
* @param timeOut 请求的超时时间设置
152151
* @param mediaType 请求体的媒体类型,通常指明发送的数据格式(如"application/json")
153152
* @return 返回请求的结果,包含响应的所有信息
154153
* @throws IOException 如果请求过程中发生错误,可能会抛出此异常
155154
*/
156-
public static Result<String> post(String url, String params, Map<String, String> headers, TimeOut timeOut, MediaType mediaType) throws IOException {
155+
public static <T> Result<String> post(String url, T params, Map<String, String> headers, TimeOut timeOut, MediaType mediaType) throws IOException {
157156
log.info("请求url:{},请求入参:{},请求头部:{}", url, params, headers);
158-
RequestBody requestBody = RequestBody.create(params, mediaType);
157+
RequestBody requestBody;
158+
// 判断请求参数类型
159+
if (params instanceof String) {
160+
requestBody = RequestBody.create((String) params, mediaType);
161+
} else {
162+
requestBody = RequestBody.create(JSON.toJSONString(params), mediaType);
163+
}
159164
Request request = buildRequest(url, headers, requestBody, RequestMethod.POST);
160165
OkHttpClient okHttpClient = buildOkHttpClient(url, timeOut);
161166
return buildResult(okHttpClient, request);
@@ -192,8 +197,8 @@ public static Result<String> upload(String url, File file, Map<String, String> h
192197
/**
193198
* 文件上传接口
194199
*
195-
* @param url 文件上传的URL,指定文件将被上传到的服务器地址
196-
* @param file 待上传的文件,通过File对象指定
200+
* @param url 文件上传的URL,指定文件将被上传到的服务器地址
201+
* @param file 待上传的文件,通过File对象指定
197202
* @param headers 请求头信息,用于设置HTTP请求的头字段
198203
* @param timeOut 超时设置,控制请求的读写超时时间
199204
* @return 返回一个Result对象,包含上传结果和一个字符串消息
@@ -213,7 +218,7 @@ public static Result<String> upload(String url, File file, Map<String, String> h
213218
* 构建httpClient
214219
* 根据给定的URL和超时设置构建一个OkHttpClient实例,支持HTTPS连接和自定义超时设置
215220
*
216-
* @param url 要请求的URL,用于确定使用HTTP还是HTTPS
221+
* @param url 要请求的URL,用于确定使用HTTP还是HTTPS
217222
* @param timeOut 超时设置,用于设置连接、读取和写入的超时时间
218223
* @return 构建好的OkHttpClient实例
219224
*/
@@ -234,10 +239,10 @@ private static OkHttpClient buildOkHttpClient(String url, TimeOut timeOut) {
234239
/**
235240
* 构建请求
236241
*
237-
* @param url 请求的URL地址
238-
* @param headers 请求头信息,为键值对形式的Map
242+
* @param url 请求的URL地址
243+
* @param headers 请求头信息,为键值对形式的Map
239244
* @param requestBody 请求体内容,根据不同请求方法包含不同的数据
240-
* @param method 请求方法,包括GET、POST、PUT、PATCH、DELETE等
245+
* @param method 请求方法,包括GET、POST、PUT、PATCH、DELETE等
241246
* @return 返回构建完成的Request对象
242247
*/
243248
private static Request buildRequest(String url, Map<String, String> headers, RequestBody requestBody, RequestMethod method) {
@@ -277,15 +282,14 @@ private static Request buildRequest(String url, Map<String, String> headers, Req
277282
*/
278283
private static Result<String> buildResult(OkHttpClient okHttpClient, Request request) throws IOException {
279284
try (Response response = okHttpClient.newCall(request).execute()) {
280-
if (!response.isSuccessful()) {
281-
throw new MyRuntimeException(MessageFormat.format("错误:OkHttp请求异常。请求:{0} 结果:{1}"
282-
, request.body(), response.body()));
285+
log.info("请求执行完成,响应状态码:{},响应信息:{}", response.code(), response.message());
286+
if (response.body() != null) {
287+
return Result.buildResult(response.body().string(), response.code(), response.message());
283288
}
284-
assert response.body() != null;
285-
return Result.buildResult(response.body().string(), response.code(), response.message());
289+
return Result.buildResult(null, response.code(), response.message());
286290
} catch (IOException e) {
287291
log.error("错误:OkHttp请求异常", e);
288-
throw e;
292+
throw new MyRuntimeException(e);
289293
}
290294
}
291295

mf-oauth/src/main/java/cn/com/mfish/oauth/filter/TokenFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
3030
HttpServletResponse response = (HttpServletResponse) servletResponse;
3131
response.setContentType(ContentType.APPLICATION_JSON.toString());
3232
response.setStatus(HttpStatus.UNAUTHORIZED.value());
33-
response.getWriter().println(result);
33+
response.getWriter().print(result);
3434
}
3535

3636
protected Result<?> isAccessAllowed(ServletRequest request) {

0 commit comments

Comments
 (0)