private Optional<String> getResponseJson(RestApi restApi){
try {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLiCATION_JSON);
if(Optional.ofNullable(restApi.getAuthorizaton()).isPresent()){
httpHeaders.add("Authorization",restApi.getAuthrization());
}
httpentity<String> requestEntity = new httpentity<>(null,httpHeaders);
ResponeseEntity<String> responseEntity = restTemplate.exchange(restApi.getUrl(),restApi.getHttpMethod(),requestEntity,String.class);
return Optional.of(responseEntity.getBody());
}catch (Exception e){
log.error();
return Optional.empty();
}
}
private Optional<String> getResponseJson(RestApi restApi,DataMapping dataMapping,Map<String,Object> params){
try {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLiCATION_JSON);
if(Optional.ofNullable(restApi.getAuthorizaton()).isPresent()){
httpHeaders.add("Authorization",restApi.getAuthrization());
}
Map<String,Object> data = new HashMap<>();
if(dataMapping.getSourceDimCode() instanceof Map){
data.put((Map)dataMapping.getSourceDimCode());
}else{
data.putAll("sourceDimCode",dataMapping.getSourceDimCode());
}
if(Optional.ofNullable(params).isPresent()){
data.putAll(params);
}
String body = render(Optional.ofNullable(restApi.getBody()).orElse(""),data);
String url = render(restApi.getUrl(),data);
httpentity<String> requestEntity = new httpentity<>(body,httpHeaders);
ResponeseEntity<String> responseEntity = restTemplate.exchange(restApi.getUrl(),restApi.getHttpMethod(),requestEntity,String.class);
return Optional.of(responseEntity.getBody());
}catch (Exception e){
log.error();
return Optional.empty();
}
}
private String render(String template,Map<String,Object> data){
try {
StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader,cfg);
org.beetl.core.Template t = gt.getTemplate(template);
t.binding(data);
String str = t.render();
return str;
}catch (IoException e){
throw new AppException();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。