Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
cn.acyou.leo.framework.util.HttpUtil2 Maven / Gradle / Ivy
package cn.acyou.leo.framework.util;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.*;
/**
* HTTP Client
*
* @author youfang
*/
public class HttpUtil2 {
public static final RestTemplate restTemplate;
private HttpUtil2(){
}
static {
restTemplate = new RestTemplate();
}
/**
* get请求
*
* @param url url
* @return 返回类型
*/
public static String get(String url) {
return get(url, null, null);
}
/**
* get请求
*
* @param url url
* @param paramsMap 参数
* @return 返回类型
*/
public static String getParameter(String url, Map paramsMap) {
return get(url, paramsMap, null);
}
/**
* get请求
*
* @param url url
* @param headersMap Header
* @return 返回类型
*/
public static String getHeader(String url, Map headersMap) {
return get(url, null, headersMap);
}
/**
* get请求
*
* @param url url
* @param paramsMap 参数
* @param headersMap Header
* @return 返回类型
*/
public static String get(String url, Map paramsMap, Map headersMap) {
return get(url, paramsMap, headersMap, String.class);
}
/**
* get请求
*
* @param url url
* @param paramsMap 参数
* @param headersMap Header
* @param resType 返回类型
* @return 返回类型
*/
public static T get(String url, Map paramsMap, Map headersMap, Class resType) {
return getMethod(url, paramsMap, headersMap, resType, null);
}
/**
* get请求
*
* @param url url
* @param paramsMap 参数
* @param headersMap Header
* @param responseType 返回类型(泛型)
* @return 返回类型
*/
public static T get(String url, Map paramsMap, Map headersMap, ParameterizedTypeReference responseType) {
return getMethod(url, paramsMap, headersMap, null, responseType);
}
/**
* get请求
*
* @param url url
* @param paramsMap 参数
* @param headersMap Header
* @param responseType 返回类型(泛型)
* @return 返回类型
*/
private static T getMethod(String url, Map paramsMap, Map headersMap, Class resType, ParameterizedTypeReference responseType) {
HttpHeaders headers = new HttpHeaders();
if (headersMap != null && !headersMap.isEmpty()) {
headersMap.forEach(headers::add);
}
if (paramsMap != null && !paramsMap.isEmpty()) {
url = UrlUtil.append(url, paramsMap);
}
HttpEntity> entity = new HttpEntity<>(headers);
if (resType != null) {
ResponseEntity result = restTemplate.exchange(url, HttpMethod.GET, entity, resType);
return result.getBody();
}
if (responseType != null) {
ResponseEntity result = restTemplate.exchange(url, HttpMethod.GET, entity, responseType);
return result.getBody();
}
return null;
}
/**
* post请求
*
* @param url url
* @param paramsMap 参数
* @param headersMap Header
* @param resType 返回类型
* @return 返回类型
*/
public static T post(String url, Map paramsMap, Map headersMap, Class resType) {
return postMethod(url, paramsMap, headersMap, resType, null);
}
/**
* post请求
*
* @param url url
* @param paramsMap 参数
* @param headersMap Header
* @param responseType 返回类型(泛型)
* @return 返回类型
*/
public static T post(String url, Map paramsMap, Map headersMap, ParameterizedTypeReference responseType) {
return postMethod(url, paramsMap, headersMap, null, responseType);
}
/**
* post 表单提交。 application/x-www-form-urlencoded
*
* @param url url
* @param paramsMap 参数映射
* @param headersMap 头图
* @param resType res类型
* @return 返回类型
*/
private static T postMethod(String url, Map paramsMap, Map headersMap, Class resType, ParameterizedTypeReference responseType) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
if (headersMap != null && !headersMap.isEmpty()) {
headersMap.forEach(headers::add);
}
MultiValueMap map = new LinkedMultiValueMap<>();
if (paramsMap != null && !paramsMap.isEmpty()) {
for (Map.Entry param : paramsMap.entrySet()) {
map.add(param.getKey(), param.getValue().toString());
}
}
HttpEntity> entity = new HttpEntity<>(map, headers);
if (resType != null) {
ResponseEntity result = restTemplate.exchange(url, HttpMethod.POST, entity, resType);
return result.getBody();
}
if (responseType != null) {
ResponseEntity result = restTemplate.exchange(url, HttpMethod.POST, entity, responseType);
return result.getBody();
}
return null;
}
/**
* postJson 请求
*
* @param url url
* @param jsonParamStr json字符串参数
* @param headersMap Header
* @param resType 返回类型
* @return 返回类型
*/
public static T postJson(String url, String jsonParamStr, Map headersMap, Class resType) {
return postJsonMethod(url, jsonParamStr, headersMap, resType, null);
}
/**
* postJson 请求
*
* @param url url
* @param jsonParamStr json字符串参数
* @param headersMap Header
* @param responseType 返回类型(泛型)
* @return 返回类型
*/
public static T postJson(String url, String jsonParamStr, Map headersMap, ParameterizedTypeReference responseType) {
return postJsonMethod(url, jsonParamStr, headersMap, null, responseType);
}
/**
* post json提交。 application/json
*
* @param url url
* @param jsonParamStr json字符串参数
* @param headersMap header
* @param resType 返回类型
* @return 返回类型
*/
private static T postJsonMethod(String url, String jsonParamStr, Map headersMap, Class resType, ParameterizedTypeReference responseType) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
if (headersMap != null && !headersMap.isEmpty()) {
headersMap.forEach(headers::add);
}
HttpEntity entity = new HttpEntity<>(jsonParamStr, headers);
if (resType != null) {
ResponseEntity result = restTemplate.exchange(url, HttpMethod.POST, entity, resType);
return result.getBody();
}
if (responseType != null) {
ResponseEntity result = restTemplate.exchange(url, HttpMethod.POST, entity, responseType);
return result.getBody();
}
return null;
}
}