Skip to content

Commit 6608e27

Browse files
committed
feat(HttpUtil): HttpUtil 工具类添加header 请求header参数
1 parent 6856b07 commit 6608e27

File tree

4 files changed

+185
-21
lines changed

4 files changed

+185
-21
lines changed

admin4j-common-http/src/main/java/io/github/admin4j/http/ApiJsonClient.java

+104-20
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ protected <T> T handleFailResponse(Response response, Class<T> tClass) {
168168
return null;
169169
}
170170

171-
//protected JSONObject serialize(Response response) {
171+
// protected JSONObject serialize(Response response) {
172172
// ResponseBody body = response.body();
173173
// if (body == null) {
174174
// throw new HttpException("response body is null");
@@ -181,7 +181,7 @@ protected <T> T handleFailResponse(Response response, Class<T> tClass) {
181181
// }
182182
//}
183183

184-
//protected JSONArray serializeList(Response response) {
184+
// protected JSONArray serializeList(Response response) {
185185
// ResponseBody body = response.body();
186186
// if (body == null) {
187187
// throw new HttpException("response body is null");
@@ -206,44 +206,61 @@ public <T> T get(String path, Map<String, Object> queryMap, Class<T> tClass) {
206206
}
207207

208208

209+
public JSONMapper get(String path, Pair<?>... queryParams) {
210+
Response response = get(path, (Map<String, Object>) null, queryParams);
211+
return handleResponse(response, JSONMapper.class);
212+
}
213+
214+
public JSONMapper get(String path, Map<String, Object> queryMap) {
215+
Response response = get(path, queryMap, (Pair<?>[]) null);
216+
return handleResponse(response, JSONMapper.class);
217+
}
218+
219+
public <T> List<T> getList(String path, Class<T> tClass, Pair<?>... queryParams) {
220+
Response response = get(path, (Map<String, Object>) null, queryParams);
221+
222+
return (List<T>) handleResponse(response, tClass, true);
223+
}
224+
225+
public <T> List<T> getList(String path, Map<String, Object> queryMap, Class<T> tClass) {
226+
Response response = get(path, queryMap, (Pair<?>[]) null);
227+
return (List<T>) handleResponse(response, tClass, true);
228+
}
229+
209230
public <T> T postForm(String url, Map<String, Object> formParams, Class<T> tClass) {
210231

211232
Response response = post(url, MediaTypeEnum.FORM, null, formParams, null);
212233
return handleResponse(response, tClass);
213234
}
214235

215-
public <T> T postFormData(String url, Map<String, Object> formParams, Class<T> tClass) {
236+
public <T> T postForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams, Class<T> tClass) {
216237

217-
Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, null);
238+
Response response = post(url, MediaTypeEnum.FORM, null, formParams, headerParams);
218239
return handleResponse(response, tClass);
219240
}
220241

221-
public <T> T post(String url, Object body, Class<T> tClass) {
242+
public <T> T postFormData(String url, Map<String, Object> formParams, Class<T> tClass) {
222243

223-
Response response = post(url, MediaTypeEnum.JSON, body, null, null);
244+
Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, null);
224245
return handleResponse(response, tClass);
225246
}
226247

248+
public <T> T postFormData(String url, Map<String, Object> formParams, Map<String, Object> headerParams, Class<T> tClass) {
227249

228-
public JSONMapper get(String path, Pair<?>... queryParams) {
229-
Response response = get(path, (Map<String, Object>) null, queryParams);
230-
return handleResponse(response, JSONMapper.class);
231-
}
232-
233-
public JSONMapper get(String path, Map<String, Object> queryMap) {
234-
Response response = get(path, queryMap, (Pair<?>[]) null);
235-
return handleResponse(response, JSONMapper.class);
250+
Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, headerParams);
251+
return handleResponse(response, tClass);
236252
}
237253

238-
public <T> List<T> getList(String path, Class<T> tClass, Pair<?>... queryParams) {
239-
Response response = get(path, (Map<String, Object>) null, queryParams);
254+
public <T> T post(String url, Object body, Class<T> tClass) {
240255

241-
return (List<T>) handleResponse(response, tClass, true);
256+
Response response = post(url, MediaTypeEnum.JSON, body, null, null);
257+
return handleResponse(response, tClass);
242258
}
243259

244-
public <T> List<T> getList(String path, Map<String, Object> queryMap, Class<T> tClass) {
245-
Response response = get(path, queryMap, (Pair<?>[]) null);
246-
return (List<T>) handleResponse(response, tClass, true);
260+
public <T> T post(String url, Object body, Map<String, Object> headerParams, Class<T> tClass) {
261+
262+
Response response = post(url, MediaTypeEnum.JSON, body, null, headerParams);
263+
return handleResponse(response, tClass);
247264
}
248265

249266

@@ -253,48 +270,96 @@ public JSONMapper postForm(String url, Map<String, Object> formParams) {
253270
return handleResponse(response, JSONMapper.class);
254271
}
255272

273+
public JSONMapper postForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {
274+
275+
Response response = post(url, MediaTypeEnum.FORM, null, formParams, headerParams);
276+
return handleResponse(response, JSONMapper.class);
277+
}
278+
256279
public JSONMapper postFormData(String url, Map<String, Object> formParams) {
257280

258281
Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, null);
259282
return handleResponse(response, JSONMapper.class);
260283
}
261284

285+
public JSONMapper postFormData(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {
286+
287+
Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, headerParams);
288+
return handleResponse(response, JSONMapper.class);
289+
}
290+
262291
public JSONMapper post(String url, Object body) {
263292

264293
Response response = post(url, MediaTypeEnum.JSON, body, null, null);
265294
return handleResponse(response, JSONMapper.class);
266295
}
267296

297+
public JSONMapper post(String url, Object body, Map<String, Object> headerParams) {
298+
299+
Response response = post(url, MediaTypeEnum.JSON, body, null, headerParams);
300+
return handleResponse(response, JSONMapper.class);
301+
}
302+
268303
public JSONMapper put(String url, Object body) {
269304

270305
Response response = put(url, MediaTypeEnum.JSON, body, null, null);
271306
return handleResponse(response, JSONMapper.class);
272307
}
273308

309+
public JSONMapper put(String url, Object body, Map<String, Object> headerParams) {
310+
311+
Response response = put(url, MediaTypeEnum.JSON, body, null, headerParams);
312+
return handleResponse(response, JSONMapper.class);
313+
}
314+
274315
public <T> T put(String url, Object body, Class<T> tClass) {
275316

276317
Response response = put(url, MediaTypeEnum.JSON, body, null, null);
277318
return handleResponse(response, tClass);
278319
}
279320

321+
public <T> T put(String url, Object body, Map<String, Object> headerParams, Class<T> tClass) {
322+
323+
Response response = put(url, MediaTypeEnum.JSON, body, null, headerParams);
324+
return handleResponse(response, tClass);
325+
}
326+
280327
public JSONMapper putForm(String url, Map<String, Object> formParams) {
281328

282329
Response response = put(url, MediaTypeEnum.FORM, null, formParams, null);
283330
return handleResponse(response, JSONMapper.class);
284331
}
285332

333+
public JSONMapper putForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {
334+
335+
Response response = put(url, MediaTypeEnum.FORM, null, formParams, headerParams);
336+
return handleResponse(response, JSONMapper.class);
337+
}
338+
286339
public <T> T putForm(String url, Map<String, Object> formParams, Class<T> tClass) {
287340

288341
Response response = put(url, MediaTypeEnum.FORM, null, formParams, null);
289342
return handleResponse(response, tClass);
290343
}
291344

345+
public <T> T putForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams, Class<T> tClass) {
346+
347+
Response response = put(url, MediaTypeEnum.FORM, null, formParams, headerParams);
348+
return handleResponse(response, tClass);
349+
}
350+
292351
public JSONMapper putFormData(String url, Map<String, Object> formParams) {
293352

294353
Response response = put(url, MediaTypeEnum.FORM_DATA, null, formParams, null);
295354
return handleResponse(response, JSONMapper.class);
296355
}
297356

357+
public JSONMapper putFormData(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {
358+
359+
Response response = put(url, MediaTypeEnum.FORM_DATA, null, formParams, headerParams);
360+
return handleResponse(response, JSONMapper.class);
361+
}
362+
298363
public <T> T delete(String url,
299364
Object body,
300365
Map<String, Object> formParams,
@@ -304,11 +369,30 @@ public <T> T delete(String url,
304369
return handleResponse(response, tClass);
305370
}
306371

372+
public <T> T delete(String url,
373+
Object body,
374+
Map<String, Object> formParams,
375+
Map<String, Object> headerParams,
376+
Class<T> tClass) {
377+
378+
Response response = delete(url, body == null ? MediaTypeEnum.FORM : MediaTypeEnum.JSON, body, formParams, headerParams);
379+
return handleResponse(response, tClass);
380+
}
381+
307382
public JSONMapper delete(String url,
308383
Object body,
309384
Map<String, Object> formParams) {
310385

311386
Response response = delete(url, body == null ? MediaTypeEnum.FORM : MediaTypeEnum.JSON, body, formParams, (Map<String, Object>) null);
312387
return handleResponse(response, JSONMapper.class);
313388
}
389+
390+
public JSONMapper delete(String url,
391+
Object body,
392+
Map<String, Object> formParams
393+
, Map<String, Object> headerParams) {
394+
395+
Response response = delete(url, body == null ? MediaTypeEnum.FORM : MediaTypeEnum.JSON, body, formParams, headerParams);
396+
return handleResponse(response, JSONMapper.class);
397+
}
314398
}

admin4j-common-http/src/main/java/io/github/admin4j/http/util/HttpJsonUtil.java

+52
Original file line numberDiff line numberDiff line change
@@ -64,40 +64,76 @@ public static JSONMapper post(String url, Object body) {
6464
return getHttpRequest().post(url, body);
6565
}
6666

67+
public static JSONMapper post(String url, Object body, Map<String, Object> headerParams) {
68+
return getHttpRequest().post(url, body, headerParams);
69+
}
70+
6771
public static <T> T post(String url, Object body, Class<T> tClass) {
6872
return getHttpRequest().post(url, body, tClass);
6973
}
7074

75+
public static <T> T post(String url, Object body, Map<String, Object> headerParams, Class<T> tClass) {
76+
return getHttpRequest().post(url, body, headerParams, tClass);
77+
}
78+
7179
public static JSONMapper postForm(String url, Map<String, Object> formParams) {
7280
return getHttpRequest().postForm(url, formParams);
7381
}
7482

83+
public static JSONMapper postForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {
84+
return getHttpRequest().postForm(url, formParams, headerParams);
85+
}
86+
7587
public static <T> T postForm(String url, Map<String, Object> formParams, Class<T> tClass) {
7688
return getHttpRequest().postForm(url, formParams, tClass);
7789
}
7890

91+
public static <T> T postForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams, Class<T> tClass) {
92+
return getHttpRequest().postForm(url, formParams, headerParams, tClass);
93+
}
94+
7995
//=================== Put =================
8096
public static JSONMapper put(String url, Object body) {
8197
return getHttpRequest().put(url, body);
8298
}
8399

100+
public static JSONMapper put(String url, Object body, Map<String, Object> headerParams) {
101+
return getHttpRequest().put(url, body, headerParams);
102+
}
103+
84104
public static <T> T put(String url, Object body, Class<T> tClass) {
85105
return getHttpRequest().put(url, body, tClass);
86106
}
87107

108+
public static <T> T put(String url, Object body, Map<String, Object> headerParams, Class<T> tClass) {
109+
return getHttpRequest().put(url, body, headerParams, tClass);
110+
}
111+
88112
public static JSONMapper putForm(String url, Map<String, Object> formParams) {
89113
return getHttpRequest().putForm(url, formParams);
90114
}
91115

116+
public static JSONMapper putForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {
117+
return getHttpRequest().putForm(url, formParams, headerParams);
118+
}
119+
92120
public static <T> T putForm(String url, Map<String, Object> formParams, Class<T> tClass) {
93121
return getHttpRequest().putForm(url, formParams, tClass);
94122
}
95123

124+
public static <T> T putForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams, Class<T> tClass) {
125+
return getHttpRequest().putForm(url, formParams, headerParams, tClass);
126+
}
127+
96128

97129
public static JSONMapper upload(String url, Map<String, Object> formParams) {
98130
return getHttpRequest().postFormData(url, formParams);
99131
}
100132

133+
public static JSONMapper upload(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {
134+
return getHttpRequest().postFormData(url, formParams, headerParams);
135+
}
136+
101137
/**
102138
* 使用 put 方法上传
103139
*
@@ -109,6 +145,10 @@ public static JSONMapper uploadPut(String url, Map<String, Object> formParams) {
109145
return getHttpRequest().putFormData(url, formParams);
110146
}
111147

148+
public static JSONMapper uploadPut(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {
149+
return getHttpRequest().putFormData(url, formParams, headerParams);
150+
}
151+
112152
// ============= delete =======
113153

114154
/**
@@ -125,6 +165,12 @@ public static JSONMapper delete(String url,
125165
return getHttpRequest().delete(url, body, formParams);
126166
}
127167

168+
public static JSONMapper delete(String url,
169+
Object body,
170+
Map<String, Object> formParams, Map<String, Object> headerParams) {
171+
return getHttpRequest().delete(url, body, formParams, headerParams);
172+
}
173+
128174
/**
129175
* 删除
130176
*
@@ -139,4 +185,10 @@ public static <T> T delete(String url,
139185
return getHttpRequest().delete(url, body, formParams, tClass);
140186
}
141187

188+
public static <T> T delete(String url,
189+
Object body,
190+
Map<String, Object> formParams, Map<String, Object> headerParams, Class<T> tClass) {
191+
return getHttpRequest().delete(url, body, formParams, headerParams, tClass);
192+
}
193+
142194
}

admin4j-common-http/src/main/java/io/github/admin4j/http/util/HttpUtil.java

+28
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,58 @@ public static Response post(String url, Object body) {
5959
return getClient().post(url, MediaTypeEnum.JSON, body, (Map<String, Object>) null, (Map<String, Object>) null);
6060
}
6161

62+
public static Response post(String url, Object body, Map<String, Object> header) {
63+
return getClient().post(url, MediaTypeEnum.JSON, body, (Map<String, Object>) null, header);
64+
}
65+
6266
public static Response postForm(String url, Map<String, Object> formParams) {
6367
return getClient().post(url, MediaTypeEnum.FORM, null, formParams, (Map<String, Object>) null);
6468
}
6569

70+
public static Response postForm(String url, Map<String, Object> formParams, Map<String, Object> header) {
71+
return getClient().post(url, MediaTypeEnum.FORM, null, formParams, header);
72+
}
73+
6674
public static Response put(String url, Object body) {
6775
return getClient().put(url, MediaTypeEnum.JSON, body, (Map<String, Object>) null, (Map<String, Object>) null);
6876
}
6977

78+
public static Response put(String url, Object body, Map<String, Object> header) {
79+
return getClient().put(url, MediaTypeEnum.JSON, body, (Map<String, Object>) null, header);
80+
}
81+
7082
public static Response putForm(String url, Map<String, Object> formParams) {
7183
return getClient().put(url, MediaTypeEnum.FORM, null, formParams, (Map<String, Object>) null);
7284
}
7385

86+
public static Response putForm(String url, Map<String, Object> formParams, Map<String, Object> header) {
87+
return getClient().put(url, MediaTypeEnum.FORM, null, formParams, header);
88+
}
89+
7490
public static Response delete(String url, Object body) {
7591
return getClient().delete(url, MediaTypeEnum.JSON, body, (Map<String, Object>) null, (Map<String, Object>) null);
7692
}
7793

94+
public static Response delete(String url, Object body, Map<String, Object> header) {
95+
return getClient().delete(url, MediaTypeEnum.JSON, body, (Map<String, Object>) null, header);
96+
}
97+
7898
public static Response deleteForm(String url, Map<String, Object> formParams) {
7999
return getClient().delete(url, MediaTypeEnum.FORM, null, formParams, (Map<String, Object>) null);
80100
}
81101

102+
public static Response deleteForm(String url, Map<String, Object> formParams, Map<String, Object> header) {
103+
return getClient().delete(url, MediaTypeEnum.FORM, null, formParams, header);
104+
}
105+
82106
public static Response upload(String url, Map<String, Object> formParams) {
83107
return getClient().post(url, MediaTypeEnum.FORM_DATA, null, formParams, (Map<String, Object>) null);
84108
}
85109

110+
public static Response upload(String url, Map<String, Object> formParams, Map<String, Object> header) {
111+
return getClient().post(url, MediaTypeEnum.FORM_DATA, null, formParams, header);
112+
}
113+
86114
public static InputStream down(String url) {
87115

88116
return getClient().down(url);

0 commit comments

Comments
 (0)