使用restTemplate调用外部接口
2017-08-05 19:19
591 查看
前提:两个服务注册到一个注册中心上面
1:在Main.class中加入@EnableDiscoveryClient注解
2:在Main.Class中加入如下代码,提供restTemplate对象
@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}3:在controller中调用外部接口
Map<String, Object> map = new HashMap<String, Object>();//map中传递参数
map.put("page", 1);map.put("pageSize", 10);String requestJson = net.sf.json.JSONObject.fromObject(map).toString();HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);HttpEntity<String> entities = new HttpEntity<String>(requestJson,headers);
String result = restTemplate.postForObject("调用外部接口的地址",entities,String.class);//这里的调用最好加入异常处理
相关文章推荐
- ribbon通过RestTemplate调用接口获取List<JavaBean>时,应使用数组而不能直接使用List
- RestTemplate使用gzip压缩调用接口
- Spring中使用RestTemplate调用第三方接口API
- JIRA的外部请求接口REST的基本使用
- java RestTemplate调用远程服务接口
- SpringBoot系列十一:SpringBoot整合Restful架构(使用 RestTemplate 模版实现 Rest 服务调用、Swagger 集成、动态修改日志级别)
- 第四章 Spring CLoud 使用RestTemplate+Eureka调用用户微服务
- SpringCloud 查找调用REST服务使用RestTemplate(ribbon负载)或feign模式 教程源码 火推
- RestEasy 调用Rest接口使用详解
- 用RestTemplate调用百度文字识别接口
- Java中使用HttpRequest调用RESTfull的DELETE方法接口提示:How to fix HTTP method DELETE doesn't support output
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- RestTemplate远程接口调用
- 使用RestTemplate来构建远程调用服务
- .NET调用外部接口将得到的List数据,并使用XmlSerializer序列化List对象成XML格式
- 使用RestTemplate实现rest服务的调用
- spring rest mvc使用RestTemplate调用
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务