resttemplate

RestTemplate中几种常见的请求方式

痞子三分冷 提交于 2020-02-02 05:58:30
GET请求 第一种:getForEntity getForEntity方法的返回值是一个 ResponseEntity<T> , ResponseEntity<T> 是Spring对HTTP请求响应的封装,包括了几个重要的元素,如响应码、contentType、contentLength、响应消息体等。 可以用一个数字做占位符,最后是一个可变长度的参数,来一一替换前面的占位符 @RequestMapping("/sayhello") public String sayHello() { ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://HELLO-SERVICE/sayhello?name={1}", String.class, "张三"); return responseEntity.getBody(); } 可以前面使用name={name}这种形式,最后一个参数是一个map,map的key即为前边占位符的名字,map的value为参数值 @RequestMapping("/sayhello2") public String sayHello2() { Map<String, String> map = new HashMap<>(); map.put("name", "李四");

ribbon负载均衡

别等时光非礼了梦想. 提交于 2020-02-02 01:51:05
负载均衡Ribbon 在刚才的案例中,我们启动了一个itcast-service-provider,然后通过DiscoveryClient来获取服务实例信息,然后获取ip和端口来访问。 但是实际环境中,我们往往会开启很多个itcast-service-provider的集群。此时我们获取的服务列表中就会有多个,到底该访问哪一个呢? 一般这种情况下我们就需要编写负载均衡算法,在多个实例列表中进行选择。 不过Eureka中已经帮我们集成了负载均衡组件:Ribbon,简单修改代码即可使用。 什么是Ribbon: 接下来,我们就来使用Ribbon实现负载均衡。 启动两个服务实例 首先参照eureka启动两个ServiceProviderApplication实例,一个8081,一个8082。 Eureka监控面板: 开启负载均衡 因为Eureka中已经集成了Ribbon,所以我们无需引入新的依赖,直接修改代码。 修改service-consumer的引导类,在RestTemplate的配置方法上添加 @LoadBalanced 注解: @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } 修改调用方式,不再手动获取ip和端口,而是直接通过服务名称调用:

负载均衡Ribbon

北战南征 提交于 2020-02-01 10:27:29
我们启动了一个 user-service ,然后通过DiscoveryClient来获取服务实例信息,然后获取ip和端口来访问。 但是实际环境中,往往会开启很多个 user-service 的集群。此时获取的服务列表中就会有多个,到底该访问哪一个 呢? 一般这种情况下就需要编写负载均衡算法,在多个实例列表中进行选择。 不过Eureka中已经集成了负载均衡组件:Ribbon,简单修改代码即可使用。 什么是Ribbon: 启动两个服务实例 首先我们配置启动两个 user-service 实例,一个9091,一个9092。 Eureka监控面板: 开启负载均衡 因为Eureka中已经集成了Ribbon,所以我们无需引入新的依赖。 直接修改 consumer-demo\src\main\java\com\itheima\consumer\ConsumerApplication.java 在RestTemplate的配置方法上添加 @LoadBalanced 注解: @Bean @LoadBalanced public RestTemplate restTemplate ( ) { return new RestTemplate ( ) ; } 修改 consumer-demo\src\main\java\com\itheima\consumer\controller

使用RestTemplate需注意

断了今生、忘了曾经 提交于 2020-02-01 03:03:55
使用RestTemplate需注意: 使用 RestTemplate 发送请求,当请求体是 String 时,应这样配置: RestTemplate restTemplate = new RestTemplate ( factory ) ; restTemplate . getMessageConverters ( ) . set ( 1 , new StringHttpMessageConverter ( StandardCharsets . UTF_8 ) ) ; 如果没有自定义 StringHttpMessageConverter ,默认的 StringHttpMessageConverter 使用的字符集是 ISO_8859_1 ,当请求体包含中文时,会乱码。 来源: CSDN 作者: 冷月醉夕阳 链接: https://blog.csdn.net/qq_31718287/article/details/103766986

restTemplate 使用

倾然丶 夕夏残阳落幕 提交于 2020-01-31 05:30:24
restTemplate 使用 restTemplate 简述RestTemplate GET 请求实践 getForObject()方法 getForEntity()方法 POST 请求实践 postForEntity 方法 ResponseEntity、HttpStatus、BodyBuilder结构 参考https://www.jianshu.com/p/27a82c494413,侵权删(仅学习后敲一遍) restTemplate 简述RestTemplate 是Spring用于同步client端的核心类,简化了与http服务的通信,并满足RestFul原则,程序代码可以给它提供URL,并提取结果。默认情况下,RestTemplate默认依赖jdk的HTTP连接工具。当然你也可以 通过setRequestFactory属性切换到不同的HTTP源,比如Apache HttpComponents、Netty和OkHttp。 RestTemplate能大幅简化了提交表单数据的难度,并且附带了自动转换JSON数据的功能,但只有理解了HttpEntity的组成结构(header与body),且理解了与uriVariables之间的差异,才能真正掌握其用法。这一点在Post请求更加突出,下面会介绍到。 GET 请求实践 getForObject()方法 getForObject(

SpringBoot 超时设置

六眼飞鱼酱① 提交于 2020-01-30 17:12:09
1.RestTemplate超时 设置配置HttpComponentsClientHttpRequestFactory中的RequestConfig属性 RestTemplateConfig: import lombok.extern.slf4j.Slf4j; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; /** * Created by qhong on 2018/9/10 17:53 **/ @Slf4j @Configuration public class RestTemplateConfig { @Bean @ConfigurationProperties(prefix = "rest

RestTemplate: How to send URL and query parameters together

淺唱寂寞╮ 提交于 2020-01-30 14:01:15
问题 I am trying to pass path param and query params in a URL but I am getting a weird error. below is the code String url = "http://test.com/Services/rest/{id}/Identifier" Map<String, String> params = new HashMap<String, String>(); params.put("id", "1234"); UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url) .queryParam("name", "myName"); String uriBuilder = builder.build().encode().toUriString(); restTemplate.exchange(uriBuilder , HttpMethod.PUT, requestEntity, class_p, params

Spring Cloud系列教程

有些话、适合烂在心里 提交于 2020-01-30 12:37:59
介绍 在微服务架构中,业务被拆分为多个微服务,服务之间的通讯基于http restful。在spring cloud中,服务调用方式有两种 1.Ribbon+RestTemplate 2.Fegin github地址:https://github.com/erlieStar/spring-cloud-learning 建一个服务生产者 示例项目:producer-simple(spring-cloud-ribbon) 1.项目配置如下 pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> 1 2 3 4 application.yaml server: port: 8001 spring: application: name: producer-simple eureka: client: service-url: defaultZone: http://localhost:7001/eureka 1 2 3 4 5 6 7 8 9 10 11 2.启动类加上@EnableEurekaClient注解 @RestController

How to Mock REST API in unit testing?

牧云@^-^@ 提交于 2020-01-30 08:19:47
问题 I am using RestTemplate exchange HttpMethod.POST method to POST to an endpoint. In my test file I am testing for success of the POST method. However with my current tests I am getting 401 Unauthorized error when POST request is made. I need help to Mock the API while making POST request in test file Here is my main file @Component public class DataTestRepo { private final RestTemplate restTemplate; private final String url; private final AllBuilder headersBuilder; public DataTestRepo(

How to Mock REST API in unit testing?

五迷三道 提交于 2020-01-30 08:18:44
问题 I am using RestTemplate exchange HttpMethod.POST method to POST to an endpoint. In my test file I am testing for success of the POST method. However with my current tests I am getting 401 Unauthorized error when POST request is made. I need help to Mock the API while making POST request in test file Here is my main file @Component public class DataTestRepo { private final RestTemplate restTemplate; private final String url; private final AllBuilder headersBuilder; public DataTestRepo(