resttemplate

Getting InputStream with RestTemplate

半世苍凉 提交于 2019-12-17 15:47:20
问题 I am using URL class to read an InputStream from it. Is there any way I can use RestTemplate for this? InputStream input = new URL(url).openStream(); JsonReader reader = new JsonReader(new InputStreamReader(input, StandardCharsets.UTF_8.displayName())); How can I get InputStream with RestTemplate instead of using URL ? 回答1: You should not get the InputStream directly. RestTemplate is meant to encapsulate processing the response (and request) content. Its strength is handling all the IO and

SpringCloud(2)服务消费者(rest+ribbon)

微笑、不失礼 提交于 2019-12-17 11:05:45
1.准备工作 这一篇文章基于上一篇文章的工程。启动eureka-server 工程,端口为 8761。分别以端口 8762 和 8763 启动 service-hi 工程。访问 localhost:8761 你会发现,service-hi 在eureka-server 注册了2个实例,这就相当于一个小的集群。 2.建1个服务消费者 重新新建一个 spring-boot 工程,取名为 service-ribbon。 在它的 pom.xml 文件分别引入起步依赖 spring-cloud-starter-eureka、spring-cloud-starter-ribbon、spring-boot-starter-web,代码如下: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

How to disable SSL certificate checking with Spring RestTemplate?

主宰稳场 提交于 2019-12-17 08:17:13
问题 I am trying to write an integration test where our test launches an embedded HTTPS server using Simple. I created a self-signed certificate using keytool and am able to access the server using a browser (specifically Chrome, and I do get a warning about the self-signed certificate). However, when I try to connect using Spring RestTemplate, I get a ResourceAccessException: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://localhost:8088":sun.security

POST request via RestTemplate in JSON

[亡魂溺海] 提交于 2019-12-17 04:13:08
问题 I didn't find any example how to solve my problem, so I want to ask you for help. I can't simply send POST request using RestTemplate object in JSON Every time I get: org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type I use RestTemplate in this way: ... restTemplate = new RestTemplate(); List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>(); list.add(new MappingJacksonHttpMessageConverter()); restTemplate.setMessageConverters(list); ...

Multipart File Upload Using Spring Rest Template + Spring Web MVC

社会主义新天地 提交于 2019-12-17 03:03:44
问题 I am trying to upload a File using RestTemplate with the following code. MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<>(); multipartMap.add("file", new ClassPathResource(file)); HttpHeaders headers = new HttpHeaders(); headers.setContentType(new MediaType("multipart", "form-data")); HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(multipartMap, headers); System.out.println("Request for File Upload : " + request);

Multipart File Upload Using Spring Rest Template + Spring Web MVC

主宰稳场 提交于 2019-12-17 03:03:13
问题 I am trying to upload a File using RestTemplate with the following code. MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<>(); multipartMap.add("file", new ClassPathResource(file)); HttpHeaders headers = new HttpHeaders(); headers.setContentType(new MediaType("multipart", "form-data")); HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(multipartMap, headers); System.out.println("Request for File Upload : " + request);

Spring RestTemplate timeout

守給你的承諾、 提交于 2019-12-17 02:41:54
问题 I would like to set the connection timeouts for a rest service used by my web application. I'm using Spring's RestTemplate to talk to my service. I've done some research and I've found and used the xml below (in my application xml) which I believe is meant to set the timeout. I'm using Spring 3.0. I've also seen the same problem here Timeout configuration for spring webservices with RestTemplate but the solutions don't seem that clean , I'd prefer to set the timeout values via Spring config

负载均衡组件Robbin

﹥>﹥吖頭↗ 提交于 2019-12-16 16:52:57
1.负载均衡Robbin Eureka中已经帮我们集成了负载均衡组件:Ribbon,简单修改代码即可使用。 什么是Ribbon: 接下来,我们就来使用Ribbon实现负载均衡。 1.1.启动两个服务实例 首先我们启动两个user-service实例,一个8081,一个8082。 Eureka监控面板: 1.2.开启负载均衡 因为Eureka中已经集成了Ribbon,所以我们无需引入新的依赖。直接修改代码: 在RestTemplate的配置方法上添加 @LoadBalanced 注解: @Bean @LoadBalanced public RestTemplate restTemplate ( ) { return new RestTemplate ( new OkHttp3ClientHttpRequestFactory ( ) ) ; } 修改调用方式,不再手动获取ip和端口,而是直接通过服务名称调用: @Service public class UserService { @Autowired private RestTemplate restTemplate ; @Autowired private DiscoveryClient discoveryClient ; public List < User > queryUserByIds ( List < Long > ids

服务调用组件Feign

*爱你&永不变心* 提交于 2019-12-16 16:43:25
1.Feign 使用了Ribbon的负载均衡功能,大大简化了远程调用时的代码: String baseUrl = "http://user-service/user/" ; User user = this . restTemplate . getForObject ( baseUrl + id , User . class ) 你可能以后需要编写类似的大量重复代码,格式基本相同,无非参数不一样。有没有更优雅的方式,来对这些代码再次优化呢? 这就是我们接下来要学的Feign的功能了。 1.1.简介 有道词典的英文解释: 为什么叫伪装? Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样。你不用再自己拼接url,拼接参数等等操作,一切都交给Feign去做。 项目主页:https://github.com/OpenFeign/feign 1.2.快速入门 1.2.1.导入依赖 < dependency > < groupId > org.springframework.cloud </ groupId > < artifactId > spring-cloud-starter-openfeign </ artifactId > </ dependency > 1.2.2.Feign的客户端 @FeignClient ( "user

从零搭建springcloud

自作多情 提交于 2019-12-16 01:09:31
开发工具:IDEA JDK1.8 sprngcloud微服务的架构基础 :生产者(client),消费者(client),注册中心/配置中心(server) 首先我们创建server,打开idea,选择左侧spring initializr,点击next springcloud是以eureka作为注册中心 ,这里要勾选Eureka Server 一直next创建 pom.xml如下,注意要修改springboot与springcloud版本,否则springboot版本太高版本不匹配会爆红报错,建议按照以下配置来。 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId>