31. Springboot中使用RestTemplate

简介:

一. 前言

官网使用说明

获取Eureka实例

1
2
3
4
public String serviceUrl() {
     InstanceInfo instance = discoveryClient.getNextServerFromEureka("STORES", false);
     return instance.getHomePageUrl();
}


步骤:


二. 导入包

pom.xml

1
2
3
4
5
6
7
8
< dependency >
     < groupId >org.springframework.cloud</ groupId >
     < artifactId >spring-cloud-starter-eureka</ artifactId >
</ dependency >
< dependency >
     < groupId >org.springframework.cloud</ groupId >
     < artifactId >spring-cloud-starter-hystrix</ artifactId >
</ dependency >


三. 修改启动Application

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@EnableCircuitBreaker
@EnableDiscoveryClient
public class HellloMain {
     
     @Bean
     @LoadBalanced
     RestTemplate restTemplate() {
         return new RestTemplate();
     }
     
     public static void main(String[] args) {
         SpringApplication.run(HelloMain.class, args);
 
     }
     
}


四. 业务使用

1
2
3
4
5
6
7
8
9
10
11
12
13
private int xxxx(String body) {
     RestTemplate restTemplate = new RestTemplate();
     HttpHeaders headers = new HttpHeaders();
     MediaType type = MediaType.parseMediaType("application/json");
     headers.setContentType(type);
         
     HttpEntity< String > formEntity = new HttpEntity< String >(body, headers);
         
     String result = restTemplate.postForObject(getEurkaClient("hello-module"), 
                 formEntity,
                 String.class);
     return 0;
}
1
2
3
4
public String getEurkaClient(String end) {
         InstanceInfo instance = discoveryClient.getNextServerFromEureka("smarthome-phihome", false);
     return instance.getHomePageUrl() + end;
}


这样就可以在应用程序之间互相调用



     本文转自rongwei84n 51CTO博客,原文链接:http://blog.51cto.com/483181/1954798,如需转载请自行联系原作者


相关文章
|
XML 编解码 Java
Spring Boot 中的 RestTemplate和Retrofit 插件很好
Spring Boot 中的 RestTemplate和Retrofit 插件很好
673 1
|
XML 编解码 Java
我为什么放弃Spring Boot 中的 RestTemplate?选择 Retrofit
我为什么放弃Spring Boot 中的 RestTemplate?选择 Retrofit
420 0
|
Java Spring
【编程笔记】在 Spring 项目中使用 RestTemplate 发送网络请求
【编程笔记】在 Spring 项目中使用 RestTemplate 发送网络请求
246 0
|
Java
SpringBoot集成RestTemplate组件
SpringBoot集成RestTemplate组件
433 0
|
11月前
|
Java 测试技术 API
在 Spring 中 Mock RestTemplate
本文介绍了两种在单元测试中 mock RestTemplate 调用的方法,避免真实 HTTP API 调用以提高测试可控性。一是使用 Mockito 模拟库,通过 @Mock 和 when/then 方法定义模拟行为;二是借助 Spring Test 提供的 MockRestServiceServer,创建模拟服务器定义请求响应交互。文中结合具体代码示例展示了两种方法的实现细节,并强调了 RestTemplate 实例的一致性配置。适用于需要模拟外部 HTTP 调用的集成测试场景。
290 1
|
Java Spring
spring restTemplate 进行http请求的工具类封装
spring restTemplate 进行http请求的工具类封装
753 3
|
文字识别 Java Python
文本,文识10,springBoot提供RestTemplate以调用Flask OCR接口,调用flask实现ocr接口,用paddleocr进行图片识别云服务技术,单个paddleocr接口有影响
文本,文识10,springBoot提供RestTemplate以调用Flask OCR接口,调用flask实现ocr接口,用paddleocr进行图片识别云服务技术,单个paddleocr接口有影响
|
Java 微服务 Spring
微服务04---服务远程调用,根据订单id查询订单功能,根据id查询订单的同时,把订单所属的用户信息一起返回,Spring提供了一个工具RestTemplate,Bean写在对象前面,以后可以在任何地
微服务04---服务远程调用,根据订单id查询订单功能,根据id查询订单的同时,把订单所属的用户信息一起返回,Spring提供了一个工具RestTemplate,Bean写在对象前面,以后可以在任何地
|
Java Spring
springboot使用RestTemplate(基于2.6.7,返回泛型)
springboot使用RestTemplate(基于2.6.7,返回泛型)
597 0
|
JSON 负载均衡 安全
【SpringBoot技术专题】「实战指南」从实战开发角度去分析操作RestTemplate的应用及使用技巧
当你的应用程序需要访问远程接口时,很容易被不同的浏览器和API调用协议弄晕。幸运的是,Spring框架已为我们提供了一个简单而功能强大的RestTemplate工具,它可以轻松地处理这些基础任务并提供一个简单的方式来访问各种API。
351 0
【SpringBoot技术专题】「实战指南」从实战开发角度去分析操作RestTemplate的应用及使用技巧