Post请求:x-www-form-urlencoded
public <T extends Serializable> T forPostFormData(String url, MultiValueMap<String, String> param, int number, int seconds,
TypeReference<T> typeReference, String paramKey) {
Assert.state(number != 0, "至少接口请求1次");
String json = "";
String[] restUrl = url.split(",");
boolean isBreak = false;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_UTF8_VALUE);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(param, headers);
ResponseEntity<String> response;
for (String var : restUrl) {
int count = 1;
while (count <= number) {
logger.debug("调用接口URL:" + var + ",param:" + param);
try {
if (count > 1 && seconds > 0) {
logger.info("接口将在" + seconds + "秒后调用");
Thread.sleep(seconds * 1000L);
}
response = restTemplate.postForEntity(var, requestEntity, String.class);
json = response.getBody();
isBreak = true;
logger.info("调用接口成功URL:" + var + ",param:"
+ ("".equals(paramKey) || null == paramKey ? param : paramKey) + ",返回数据:" + json);
break;
} catch (Exception ex) {
count++;
logger.error("调用接口失败URL:" + var + ",param:"
+ ("".equals(paramKey) || null == paramKey ? param : paramKey) + ",错误信息:" + ex.getMessage(), ex);
}
}
if (isBreak)
break;
}
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(StringUtils.isEmpty(json) ? "{}" : json, typeReference);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return null;
}
Post请求:application/json;charset=UTF-8
public <T extends Serializable> T forPostJson(String url, String param, int number, int seconds,
TypeReference<T> typeReference, String paramKey,String token) {
Assert.state(number != 0, "至少接口请求1次");
String json = "";
String[] restUrl = url.split(",");
boolean isBreak = false;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_UTF8_VALUE);
headers.add("Authority",token);
logger.info("AisinoToken:" + token);
headers.add("X-Requested-With","XMLHttpRequest");
HttpEntity<String> requestEntity = new HttpEntity<>(param, headers);
ResponseEntity<String> response;
for (String var : restUrl) {
int count = 1;
while (count <= number) {
logger.debug("调用接口URL:" + var + ",param:" + param);
try {
if (count > 1 && seconds > 0) {
logger.info("接口将在" + seconds + "秒后调用");
Thread.sleep(seconds * 1000L);
}
response = restTemplate.postForEntity(var, requestEntity, String.class);
json = response.getBody();
isBreak = true;
logger.info("调用接口成功URL:" + var + ",param:"
+ ("".equals(paramKey) || null == paramKey ? param : paramKey) + ",返回数据:" + json);
break;
} catch (Exception ex) {
count++;
logger.error("调用接口失败URL:" + var + ",param:"
+ ("".equals(paramKey) || null == paramKey ? param : paramKey) + ",错误信息:" + ex.getMessage(), ex);
}
}
if (isBreak)
break;
}
try {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(StringUtils.isEmpty(json) ? "{}" : json, typeReference);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return null;
}
网友评论