Skip to content

Commit

Permalink
feat(HttpUtil): HttpUtil 工具类添加header 请求header参数
Browse files Browse the repository at this point in the history
  • Loading branch information
andanyoung committed Nov 27, 2023
1 parent 6856b07 commit 6608e27
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected <T> T handleFailResponse(Response response, Class<T> tClass) {
return null;
}

//protected JSONObject serialize(Response response) {
// protected JSONObject serialize(Response response) {
// ResponseBody body = response.body();
// if (body == null) {
// throw new HttpException("response body is null");
Expand All @@ -181,7 +181,7 @@ protected <T> T handleFailResponse(Response response, Class<T> tClass) {
// }
//}

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


public JSONMapper get(String path, Pair<?>... queryParams) {
Response response = get(path, (Map<String, Object>) null, queryParams);
return handleResponse(response, JSONMapper.class);
}

public JSONMapper get(String path, Map<String, Object> queryMap) {
Response response = get(path, queryMap, (Pair<?>[]) null);
return handleResponse(response, JSONMapper.class);
}

public <T> List<T> getList(String path, Class<T> tClass, Pair<?>... queryParams) {
Response response = get(path, (Map<String, Object>) null, queryParams);

return (List<T>) handleResponse(response, tClass, true);
}

public <T> List<T> getList(String path, Map<String, Object> queryMap, Class<T> tClass) {
Response response = get(path, queryMap, (Pair<?>[]) null);
return (List<T>) handleResponse(response, tClass, true);
}

public <T> T postForm(String url, Map<String, Object> formParams, Class<T> tClass) {

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

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

Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, null);
Response response = post(url, MediaTypeEnum.FORM, null, formParams, headerParams);
return handleResponse(response, tClass);
}

public <T> T post(String url, Object body, Class<T> tClass) {
public <T> T postFormData(String url, Map<String, Object> formParams, Class<T> tClass) {

Response response = post(url, MediaTypeEnum.JSON, body, null, null);
Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, null);
return handleResponse(response, tClass);
}

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

public JSONMapper get(String path, Pair<?>... queryParams) {
Response response = get(path, (Map<String, Object>) null, queryParams);
return handleResponse(response, JSONMapper.class);
}

public JSONMapper get(String path, Map<String, Object> queryMap) {
Response response = get(path, queryMap, (Pair<?>[]) null);
return handleResponse(response, JSONMapper.class);
Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, headerParams);
return handleResponse(response, tClass);
}

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

return (List<T>) handleResponse(response, tClass, true);
Response response = post(url, MediaTypeEnum.JSON, body, null, null);
return handleResponse(response, tClass);
}

public <T> List<T> getList(String path, Map<String, Object> queryMap, Class<T> tClass) {
Response response = get(path, queryMap, (Pair<?>[]) null);
return (List<T>) handleResponse(response, tClass, true);
public <T> T post(String url, Object body, Map<String, Object> headerParams, Class<T> tClass) {

Response response = post(url, MediaTypeEnum.JSON, body, null, headerParams);
return handleResponse(response, tClass);
}


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

public JSONMapper postForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {

Response response = post(url, MediaTypeEnum.FORM, null, formParams, headerParams);
return handleResponse(response, JSONMapper.class);
}

public JSONMapper postFormData(String url, Map<String, Object> formParams) {

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

public JSONMapper postFormData(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {

Response response = post(url, MediaTypeEnum.FORM_DATA, null, formParams, headerParams);
return handleResponse(response, JSONMapper.class);
}

public JSONMapper post(String url, Object body) {

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

public JSONMapper post(String url, Object body, Map<String, Object> headerParams) {

Response response = post(url, MediaTypeEnum.JSON, body, null, headerParams);
return handleResponse(response, JSONMapper.class);
}

public JSONMapper put(String url, Object body) {

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

public JSONMapper put(String url, Object body, Map<String, Object> headerParams) {

Response response = put(url, MediaTypeEnum.JSON, body, null, headerParams);
return handleResponse(response, JSONMapper.class);
}

public <T> T put(String url, Object body, Class<T> tClass) {

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

public <T> T put(String url, Object body, Map<String, Object> headerParams, Class<T> tClass) {

Response response = put(url, MediaTypeEnum.JSON, body, null, headerParams);
return handleResponse(response, tClass);
}

public JSONMapper putForm(String url, Map<String, Object> formParams) {

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

public JSONMapper putForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {

Response response = put(url, MediaTypeEnum.FORM, null, formParams, headerParams);
return handleResponse(response, JSONMapper.class);
}

public <T> T putForm(String url, Map<String, Object> formParams, Class<T> tClass) {

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

public <T> T putForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams, Class<T> tClass) {

Response response = put(url, MediaTypeEnum.FORM, null, formParams, headerParams);
return handleResponse(response, tClass);
}

public JSONMapper putFormData(String url, Map<String, Object> formParams) {

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

public JSONMapper putFormData(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {

Response response = put(url, MediaTypeEnum.FORM_DATA, null, formParams, headerParams);
return handleResponse(response, JSONMapper.class);
}

public <T> T delete(String url,
Object body,
Map<String, Object> formParams,
Expand All @@ -304,11 +369,30 @@ public <T> T delete(String url,
return handleResponse(response, tClass);
}

public <T> T delete(String url,
Object body,
Map<String, Object> formParams,
Map<String, Object> headerParams,
Class<T> tClass) {

Response response = delete(url, body == null ? MediaTypeEnum.FORM : MediaTypeEnum.JSON, body, formParams, headerParams);
return handleResponse(response, tClass);
}

public JSONMapper delete(String url,
Object body,
Map<String, Object> formParams) {

Response response = delete(url, body == null ? MediaTypeEnum.FORM : MediaTypeEnum.JSON, body, formParams, (Map<String, Object>) null);
return handleResponse(response, JSONMapper.class);
}

public JSONMapper delete(String url,
Object body,
Map<String, Object> formParams
, Map<String, Object> headerParams) {

Response response = delete(url, body == null ? MediaTypeEnum.FORM : MediaTypeEnum.JSON, body, formParams, headerParams);
return handleResponse(response, JSONMapper.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,40 +64,76 @@ public static JSONMapper post(String url, Object body) {
return getHttpRequest().post(url, body);
}

public static JSONMapper post(String url, Object body, Map<String, Object> headerParams) {
return getHttpRequest().post(url, body, headerParams);
}

public static <T> T post(String url, Object body, Class<T> tClass) {
return getHttpRequest().post(url, body, tClass);
}

public static <T> T post(String url, Object body, Map<String, Object> headerParams, Class<T> tClass) {
return getHttpRequest().post(url, body, headerParams, tClass);
}

public static JSONMapper postForm(String url, Map<String, Object> formParams) {
return getHttpRequest().postForm(url, formParams);
}

public static JSONMapper postForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {
return getHttpRequest().postForm(url, formParams, headerParams);
}

public static <T> T postForm(String url, Map<String, Object> formParams, Class<T> tClass) {
return getHttpRequest().postForm(url, formParams, tClass);
}

public static <T> T postForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams, Class<T> tClass) {
return getHttpRequest().postForm(url, formParams, headerParams, tClass);
}

//=================== Put =================
public static JSONMapper put(String url, Object body) {
return getHttpRequest().put(url, body);
}

public static JSONMapper put(String url, Object body, Map<String, Object> headerParams) {
return getHttpRequest().put(url, body, headerParams);
}

public static <T> T put(String url, Object body, Class<T> tClass) {
return getHttpRequest().put(url, body, tClass);
}

public static <T> T put(String url, Object body, Map<String, Object> headerParams, Class<T> tClass) {
return getHttpRequest().put(url, body, headerParams, tClass);
}

public static JSONMapper putForm(String url, Map<String, Object> formParams) {
return getHttpRequest().putForm(url, formParams);
}

public static JSONMapper putForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {
return getHttpRequest().putForm(url, formParams, headerParams);
}

public static <T> T putForm(String url, Map<String, Object> formParams, Class<T> tClass) {
return getHttpRequest().putForm(url, formParams, tClass);
}

public static <T> T putForm(String url, Map<String, Object> formParams, Map<String, Object> headerParams, Class<T> tClass) {
return getHttpRequest().putForm(url, formParams, headerParams, tClass);
}


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

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

/**
* 使用 put 方法上传
*
Expand All @@ -109,6 +145,10 @@ public static JSONMapper uploadPut(String url, Map<String, Object> formParams) {
return getHttpRequest().putFormData(url, formParams);
}

public static JSONMapper uploadPut(String url, Map<String, Object> formParams, Map<String, Object> headerParams) {
return getHttpRequest().putFormData(url, formParams, headerParams);
}

// ============= delete =======

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

public static JSONMapper delete(String url,
Object body,
Map<String, Object> formParams, Map<String, Object> headerParams) {
return getHttpRequest().delete(url, body, formParams, headerParams);
}

/**
* 删除
*
Expand All @@ -139,4 +185,10 @@ public static <T> T delete(String url,
return getHttpRequest().delete(url, body, formParams, tClass);
}

public static <T> T delete(String url,
Object body,
Map<String, Object> formParams, Map<String, Object> headerParams, Class<T> tClass) {
return getHttpRequest().delete(url, body, formParams, headerParams, tClass);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,30 +59,58 @@ public static Response post(String url, Object body) {
return getClient().post(url, MediaTypeEnum.JSON, body, (Map<String, Object>) null, (Map<String, Object>) null);
}

public static Response post(String url, Object body, Map<String, Object> header) {
return getClient().post(url, MediaTypeEnum.JSON, body, (Map<String, Object>) null, header);
}

public static Response postForm(String url, Map<String, Object> formParams) {
return getClient().post(url, MediaTypeEnum.FORM, null, formParams, (Map<String, Object>) null);
}

public static Response postForm(String url, Map<String, Object> formParams, Map<String, Object> header) {
return getClient().post(url, MediaTypeEnum.FORM, null, formParams, header);
}

public static Response put(String url, Object body) {
return getClient().put(url, MediaTypeEnum.JSON, body, (Map<String, Object>) null, (Map<String, Object>) null);
}

public static Response put(String url, Object body, Map<String, Object> header) {
return getClient().put(url, MediaTypeEnum.JSON, body, (Map<String, Object>) null, header);
}

public static Response putForm(String url, Map<String, Object> formParams) {
return getClient().put(url, MediaTypeEnum.FORM, null, formParams, (Map<String, Object>) null);
}

public static Response putForm(String url, Map<String, Object> formParams, Map<String, Object> header) {
return getClient().put(url, MediaTypeEnum.FORM, null, formParams, header);
}

public static Response delete(String url, Object body) {
return getClient().delete(url, MediaTypeEnum.JSON, body, (Map<String, Object>) null, (Map<String, Object>) null);
}

public static Response delete(String url, Object body, Map<String, Object> header) {
return getClient().delete(url, MediaTypeEnum.JSON, body, (Map<String, Object>) null, header);
}

public static Response deleteForm(String url, Map<String, Object> formParams) {
return getClient().delete(url, MediaTypeEnum.FORM, null, formParams, (Map<String, Object>) null);
}

public static Response deleteForm(String url, Map<String, Object> formParams, Map<String, Object> header) {
return getClient().delete(url, MediaTypeEnum.FORM, null, formParams, header);
}

public static Response upload(String url, Map<String, Object> formParams) {
return getClient().post(url, MediaTypeEnum.FORM_DATA, null, formParams, (Map<String, Object>) null);
}

public static Response upload(String url, Map<String, Object> formParams, Map<String, Object> header) {
return getClient().post(url, MediaTypeEnum.FORM_DATA, null, formParams, header);
}

public static InputStream down(String url) {

return getClient().down(url);
Expand Down
Loading

0 comments on commit 6608e27

Please sign in to comment.