@Component
public class RemoteQuestUtil {
@Autowired
private RestTemplate restTemplate;
public String send(String srvcCode, String request){
//srvcCode 获取对应交易 现场适配
MockPropertiesUtil instance = MockPropertiesUtil.getInstance();
String url = instance.getProperty(srvcCode);
//处理接收接口只支持JSONOBject
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity request1 = new HttpEntity<>(request, headers);
log.info("请求报文:{}",request);
ResponseEntity<String> jsonObjectResponseEntity = restTemplate.postForEntity(url, request1, String.class);
String resp = jsonObjectResponseEntity.getBody();
log.info("响应报文:{}",resp);
return resp;
}
}
MockPropertiesUtil
public class MockPropertiesUtil {
public static Properties pro = new Properties();
public static MockPropertiesUtil instance = new MockPropertiesUtil();
public static MockPropertiesUtil getInstance() {
return instance;
}
static {
InputStream in ;
try {
in = MockPropertiesUtil.class.getClassLoader().getResourceAsStream("mock-properties.properties"); //加载文件将文件加载为
pro = new Properties();
pro.load(in);
} catch (IOException e) {
log.error("加载Mock配置文件[mock-properties.properties]时异常",e);
throw new PlatformException(PlatformError.LOADCONFIG_ERROR);
}
try {
if (in != null){
in.close();
}
} catch (IOException e) {
log.error("关闭流异常",e);
}
}
public String getProperty(String key) {
if (StringUtil.isEmpty(key)) {
log.warn("key is null.");
return null;
}
if (!pro.containsKey(key)){
log.info("无此服务码:{}",key);
throw new PlatformException(PlatformError.GET_URL_BY_CODE_ERROR);
}
//通过键值获得对应的url key=url
String url = pro.getProperty(key);
log.info("服务码:{},对应的url为:{}",key,url);
return url;
}
}