介绍
RestTemplate 是从 Spring3.0 开始支持的一个 HTTP 请求工具,它提供了常见的REST请求方案的模版,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange 以及 execute。RestTemplate 继承自 InterceptingHttpAccessor 并且实现了 RestOperations 接口,其中 RestOperations 接口定义了基本的 RESTful 操作,这些操作在 RestTemplate 中都得到了实现。本文主要讲解其它两种配置及使用
配置及使用
- 配置
RestTemplateConfig.java
@Configuration
public class RestTemplateConfig {
/**
内部请求模板
*/
@LoadBalanced
@Bean(value = {"restTemplateFeign"})
public RestTemplate restTemplateFeign() {
return new RestTemplate();
}
/**
普通请求模板
*/
@Bean(value = {"restTemplate"})
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
RestTemplateConfig.java 中配置了两个请求模板,restTemplateFeign配置的是负载均衡的内部请求模板,restTemplate配置为通用的请求模板。
- 使用
restTemplateFeign 的使用
内部调用部分代码
@Autowired
private RestTemplate restTemplateFeign;
//内部url
String INNER_URL = "http://xxxfile/identity/factorAuth";
//三要素认证2
private boolean factorAuth0(String name, String idcard, String mobile){
if (StringUtils.isEmpty(name)) {
throw new CustomException("三要素认证,用户姓名不能为空");
}
if (StringUtils.isEmpty(idcard)) {
throw new CustomException("三要素认证,身份证号码不能为空");
}
if (StringUtils.isEmpty(mobile)) {
throw new CustomException("三要素认证,手机号不能为空");
}
//是否开启三要素认证
SysConfig config = new SysConfig();
config.setConfigKey("open.factor.auth");
SysConfig retConfig = configMapper.selectConfig(config);
String openAuth = retConfig.getConfigValue();
if("0".equals(openAuth)){
return true;
}
boolean authResult = false;
JSONObject jsonObject = new JSONObject();
jsonObject.put("name",name);
jsonObject.put("idcard", idcard);
jsonObject.put("mobile", mobile);
log.info("开始三要素认证,请求参数:" + jsonObject);
AjaxResult ajaxResult = null;
try{
ajaxResult = restTemplateFeign.postForObject(INNER_URL,jsonObject,AjaxResult.class);
log.info("结束三要素认证,返回参数:" + ajaxResult);
if(ajaxResult != null){
if("200".equals(String.valueOf(ajaxResult.get("code")))){
JSONObject data = JSONObject.parseObject(JSON.toJSONString(ajaxResult.get("data")));
Integer status = (Integer)data.get("status");
if(status != null && status != 1){
throw new CustomException((String)data.get("errReason"));
}
if(status == null){
throw new CustomException("三要素认证失败");
}
authResult = true;
} else {
throw new CustomException(String.valueOf(ajaxResult.get("msg")), (int)(ajaxResult.get("code")));
}
}
} catch (Exception e) {
log.error("三要素认证,异常信息:" + e);
if(e instanceof CustomException){
throw new CustomException(e.getMessage(),((CustomException) e).getCode());
}
throw new CustomException("三要素认证失败,请稍后重试");
}
return authResult;
}
外部调用部分代码
@Autowired
private RestTemplate restTemplate;
String OUT_URL= https://ydoa.gzairports.com/login
@Override
public JSONObject ssoLogin(String ticket) {
log.info("致信xxx调用登录ticket:{}", ticket);
String url = seeyonUrl + ticket;
log.info("致信xxx调用登录url:{}", OUT_URL);
String reson = restTemplate.getForObject(OUT_URL, String.class);
log.info("致信xxx调用成功参数:{}", reson);
if(org.springframework.util.StringUtils.isEmpty(reson)){
throw new CustomException("用户不存在!");
}
String[] split = reson.split("\n");
JSONObject jsonObject = ssoLoginByUserCode(split[0]);
return jsonObject;
}
总结
如果使用负载均衡模板去请求外部url会报错,找到对应的服务,这个问题在注入模板时的变量名要和配置时候保持一致。