已知调用接口:http://www.ccb.com/userInfoConstruct/getUserInfo,传参方式为body形式。参数封装
@Data
public class UserDto {
private String uniqueCode;
private String accountCode;
private String name;
private String accountLevel;
}
参数形式:body,请求方式 post
public class RestTemplateUtilsDemo {
public static void main(String[] args) {
String url = "http://www.ccb.com/userInfoConstruct/getUserInfo";
UserDto dto = new UserDto();
dto.setAccountCode("15835965487965649466154");
dto.setName("至尊人物,贷款要降低至1.8个点+送一瓶飞天");
dto.setUniqueCode("15835965487965649466154");
dto.setAccountLevel("至尊");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
httpHeaders.add("Authorization", "Bearer " + "token");
httpHeaders.add("bankSign", "bankSign");
httpHeaders.add("level", dto.getAccountLevel());
log.info("请求建行用户信息:链接:{},dto:{}",url, JSON.toJSONString(dto));
String jsonObject = RestTemplateUtils.post(null, url, dto, httpHeaders, String.class);
log.info("请求建行用户信息:{}",JSON.toJSONString(jsonObject));
}
}
调用接口:http://www.ccb.com/userInfoConstruct/queryAccountBalance,传参方式为query形式,参数封装
@Data
public class AccountBalanceDto {
private String uniqueCode;
private String accountCode;
private String accountNumber;
}
参数形式:query,请求方式 post
public class RestTemplateUtilsDemo {
public static void main(String[] args) {
String url = "http://www.ccb.com/userInfoConstruct/queryAccountBalance";
AccountBalanceDto balanceDto = new AccountBalanceDto();
balanceDto.setAccountCode("15835965487965649466154");
balanceDto.setUniqueCode("15835965487965649466154");
balanceDto.setAccountNumber("6217487965649466154");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
httpHeaders.add("Authorization", "Bearer " + "token");
httpHeaders.add("bankSign", "bankSign");
LinkedMultiValueMap<String, String> queryParamMap = new LinkedMultiValueMap<>();
queryParamMap.add("uniqueCode",balanceDto.getUniqueCode());
queryParamMap.add("accountCode",balanceDto.getAccountCode());
queryParamMap.add("accountNumber",balanceDto.getAccountNumber());
log.info("请求账户余额:链接:{},dto:{}",url, JSON.toJSONString(queryParamMap));
String jsonObject = RestTemplateUtils.post(null, url, queryParamMap, httpHeaders, String.class);
log.info("请求账户余额结果:{}",JSON.toJSONString(jsonObject));
}
}
query的原始形式:
获取账户的余额
String url = "http://www.ccb.com/userInfoConstruct/queryAccountBalance?uniqueCode=xxx&accountCode=xxx&accountNumber=xxxx";
public class RestTemplateUtilsDemo {
public static void main(String[] args) {
String url = "http://www.ccb.com/userInfoConstruct/queryAccountBalance";
AccountBalanceDto balanceDto = new AccountBalanceDto();
balanceDto.setAccountCode("15835965487965649466154");
balanceDto.setUniqueCode("15835965487965649466154");
balanceDto.setAccountNumber("6217487965649466154");
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
httpHeaders.add("Authorization", "Bearer " + "token");
httpHeaders.add("bankSign", "bankSign");
StringBuilder sb = new StringBuilder();
sb.append(url)
.append("?uniqueCode=")
.append(balanceDto.getUniqueCode())
.append("&accountCode=")
.append(balanceDto.getAccountCode())
.append("&accountNumber=")
.append(balanceDto.getAccountNumber());
log.info("请求账户余额:链接:{},dto:{}",url, JSON.toJSONString(balanceDto));
String jsonObject = RestTemplateUtils.post(null, sb.toString(),null,null, httpHeaders, String.class);
log.info("请求账户余额结果:{}",JSON.toJSONString(jsonObject));
}
}
根据业务不同,传参不同,来分别满足自己的业务即可。