import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.thunisoft.t3.sscyr.template.impl.GetRETemplateImpl;
import com.thunisoft.t3.sscyr.template.impl.PatchRETemplateImpl;
import com.thunisoft.t3.sscyr.template.impl.PostRETemplateImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public final class RETemplate {
@Autowired
private PostRETemplateImpl postService;
@Autowired
private GetRETemplateImpl getService;
@Autowired
private PatchRETemplateImpl patchService;
private RETemplate () {
}
public String Post (String paramType, String url, Object param, Map<String, String> headers) {
String response = null;
switch (paramType) {
case ParamTypeConst.STRING:
response = postService.postTemplate(url, param.toString (), headers);
break;
case ParamTypeConst.Map:
response = postService.postTemplate(url, JSONObject.parseObject (JSONObject.toJSONString (param), HashMap.class), headers);
break;
case ParamTypeConst.JSON:
response = postService.postTemplate(url, JSONObject.parseObject (JSONObject.toJSONString (param)), headers);
break;
case ParamTypeConst.XML:
response = postService.postTemplate(url, param.toString ());
break;
case ParamTypeConst.JSONSTRING:
response = postService.postTemplate(url, JSON.toJSONString (param), headers);
break;
case ParamTypeConst.LIST:
response = postService.postTemplate(url, (List) param, headers);
break;
default:
break;
}
return response;
}
public String Get (String paramType, String url, Object param, Map<String, String> headers) {
String response = null;
switch (paramType) {
case ParamTypeConst.NULL:
response = getService.getTemplate(url, headers);
break;
case ParamTypeConst.Map:
response = getService.getTemplate(url, JSONObject.parseObject (JSONObject.toJSONString (param), Map.class), headers);
break;
default:
break;
}
return response;
}
public String Patch (String paramType, String url, Object param, Map<String, String> headers) {
String response = null;
switch (paramType) {
case ParamTypeConst.STRING:
response = patchService.patchTemplate(url, param.toString (), headers);
break;
case ParamTypeConst.JSON:
response = patchService.patchTemplate(url, JSONObject.parseObject (JSONObject.toJSONString (param)), headers);
break;
default:
break;
}
return response;
}
}
=============================================================================================================================
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public interface PostRETemplate {
String postTemplate(String url, HashMap param, Map<String, String> headers);
String postTemplate(String url, JSON param, Map<String, String> headers);
String postTemplate(String url, String param, Map<String, String> headers);
String postTemplate(String url, List param, Map<String, String> headers);
String postTemplate(String url, String param);
}
======================================================================================================
import com.alibaba.fastjson.JSON;
import java.util.Map;
public interface PatchRETemplate {
String patchTemplate(String url, JSON param, Map<String, String> headers);
String patchTemplate(String url, String param, Map<String, String> headers);
}
======================================================================================================
public final class ParamTypeConst {
/**
* 私有构造函数
*/
private ParamTypeConst() {}
public static final String STRING = "string";
public static final String JSON = "json";
public static final String JSONSTRING = "jsonstring";
public static final String LIST = "list";
public static final String Map = "map";
public static final String XML = "xml";
public static final String NULL = "null";
}
======================================================================================================
import java.util.Map;
public interface GetRETemplate {
String getTemplate(String url, Map<String, String> headers);
String getTemplate(String url, Map<String, Object> paramMap, Map<String, String> headers);
}
}
======================================================================================================
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.thunisoft.t3.sscyr.template.PostRETemplate;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class PostRETemplateImpl implements PostRETemplate {
@Autowired
private RestTemplate restTemplate;
@SneakyThrows
@Override
public String postTemplate(String url, HashMap param, Map<String, String> headers) {
// 设置头部信息
HttpHeaders requestHeaders = new HttpHeaders();
if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
requestHeaders.add(header.getKey(), header.getValue());
}
}
HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class,param);
return response.getBody();
}
@Override
public String postTemplate(String url, JSON param, Map<String, String> headers) {
// 设置头部信息
HttpHeaders requestHeaders = new HttpHeaders();
if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
requestHeaders.add(header.getKey(), header.getValue());
}
}
HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class,param);
return response.getBody();
}
@Override
public String postTemplate(String url, String param, Map<String, String> headers) {
// 设置头部信息
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Accept", "application/json");
requestHeaders.add("Content-Type", "application/json");
if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
requestHeaders.add(header.getKey(), header.getValue());
}
}
HttpEntity<String> requestEntity = new HttpEntity<String>(JSON.parseObject(param).toJSONString(), requestHeaders);
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
//ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
return response.getBody();
}
@Override
public String postTemplate(String url, String param) {
// 设置头部信息
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Accept", "text/xml");
requestHeaders.add("Accept", "application/json");
requestHeaders.add("Content-Type", "text/xml");
HttpEntity<String> requestEntity = new HttpEntity<String>(param, requestHeaders);
return restTemplate.postForObject(url, requestEntity, String.class);
}
@Override
public String postTemplate(String url, List param, Map<String, String> headers) {
// 设置头部信息
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Accept", "application/json");
requestHeaders.add("Content-Type", "application/json");
if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
requestHeaders.add(header.getKey(), header.getValue());
}
}
HttpEntity<String> requestEntity = new HttpEntity<String>(JSON.toJSONString (param, SerializerFeature.WriteNullListAsEmpty), requestHeaders);
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
//ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
return response.getBody();
}
}
======================================================================================================
import com.alibaba.fastjson.JSON;
import com.thunisoft.t3.sscyr.template.PatchRETemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Service
public class PatchRETemplateImpl implements PatchRETemplate {
@Autowired
private RestTemplate restTemplate;
@Override
public String patchTemplate(String url, JSON param, Map<String, String> headers) {
// 设置头部信息
HttpHeaders requestHeaders = new HttpHeaders();
if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
requestHeaders.add(header.getKey(), header.getValue());
}
}
HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PATCH, requestEntity, String.class,param);
return response.getBody();
}
@Override
public String patchTemplate(String url, String param, Map<String, String> headers) {
// 设置头部信息
HttpHeaders requestHeaders = new HttpHeaders();
if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
requestHeaders.add(header.getKey(), header.getValue());
}
}
HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.PATCH, requestEntity, String.class,param);
return response.getBody();
}
}
======================================================================================================
import com.thunisoft.t3.sscyr.template.GetRETemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Service
public class GetRETemplateImpl implements GetRETemplate {
@Autowired
private RestTemplate restTemplate;
@Override
public String getTemplate(String url, Map<String, String> headers) {
// 设置头部信息
HttpHeaders requestHeaders = new HttpHeaders();
if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
requestHeaders.add(header.getKey(), header.getValue());
}
}
HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
return response.getBody();
}
@Override
public String getTemplate(String url, Map<String, Object> paramMap, Map<String, String> headers) {
// 设置头部信息
HttpHeaders requestHeaders = new HttpHeaders();
if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
requestHeaders.add(header.getKey(), header.getValue());
}
}
HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class,paramMap);
return response.getBody();
}
}