67,513
社区成员
发帖
与我相关
我的任务
分享
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.JsonProcessingException;
@Service
public class CustomRestTemplate extends RestTemplate {
private final Logger logger = LoggerFactory.getLogger(CustomRestTemplate.class);
@Autowired
CommonProperties commonProperties;
@Autowired
RestTemplate restTemplate;
/**
* 提交服务器
*
* @param uri 地址
* @param map 头部
* @param hashMap 表单参数
* @return
* @throws JsonProcessingException
*/
public String custom_postForObject(String uri, Map<String, String> map) throws JsonProcessingException {
HttpHeaders header = new HttpHeaders();
MediaType mediaType = MediaType.parseMediaType("application/json;charset=utf-8");
header.setContentType(mediaType);
header.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
// 1、URL地址
String url = commonProperties.getPhp_root() + uri;
if (map != null) {
Set<String> set = new HashSet<String>();
for(Entry<String, String> item:map.entrySet())
set.add(item.getKey()+"="+item.getValue());
url = url +"?"+ StringUtils.join(set,"&");
}
// 2、提交
String result = "";
try{
result = restTemplate.postForObject(url, null, String.class);
}
catch(Exception ex)
{
logger.error(ex.getMessage() + ";url:"+ url);
}
return result;
}
/**
* 提交服务器
*
* @param uri 地址
* @param headerParams 头部
* @param bodyJsonParams 表单参数
* @return
* @throws JsonProcessingException
*/
public String custom_postForm(String uri, Map<String, String> headerParams, Map<String, String> bodyFormParams) throws JsonProcessingException {
// 1、URL地址
String url = commonProperties.getPhp_root() + uri;
if (headerParams != null) {
Set<String> set = new HashSet<String>();
for(Entry<String,String> item:headerParams.entrySet())
set.add(item.getKey()+"="+item.getValue());
url = url +"?"+ StringUtils.join(set,"&");
}
// 2、Header
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// 3、Body
MultiValueMap<String, String> body= new LinkedMultiValueMap<String, String>();
for(Entry<String,String> item:bodyFormParams.entrySet())
body.add(item.getKey(), item.getValue());
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(body, headers);
String data ="";
try
{
ResponseEntity<String> response = restTemplate.postForEntity( url, request , String.class );
data = response.getBody();
}
catch(Exception ex)
{
logger.error(ex.getMessage());
}
return data;
}
/**
* 提交服务器
*
* @param uri 地址
* @param headerParams 头部
* @param bodyJsonParams 表单参数
* @return
* @throws JsonProcessingException
*/
public String custom_postForObject(String uri, Map<String, String> headerParams, String bodyJsonParams) throws JsonProcessingException {
HttpHeaders header = new HttpHeaders();
MediaType mediaType = MediaType.parseMediaType("application/json;charset=utf-8");
header.setContentType(mediaType);
header.add("Accept", MediaType.APPLICATION_JSON_UTF8.toString());
// 1、URL地址
String url = commonProperties.getPhp_root() + uri;
if (headerParams != null) {
Set<String> set = new HashSet<String>();
for(Entry<String,String> item:headerParams.entrySet())
set.add(item.getKey()+"="+item.getValue());
url = url +"?"+ StringUtils.join(set,"&");
}
// 1、Form
HttpEntity<String> formEntity = new HttpEntity<String>("",header);
if (bodyJsonParams != null && bodyJsonParams.equals("")==false) {
formEntity = new HttpEntity<String>(bodyJsonParams,header);
}
// 2、提交
String result = "";
try {
result = restTemplate.postForObject(url, formEntity, String.class);
}
catch(Exception ex)
{
logger.error(ex.getMessage());
}
return result;
}
}