spring - 如何配置 spring 的 RestTemplate 来发出 http2 请求?

标签 spring spring-boot microservices resttemplate spring-rest

我有一个 RestClient 向 spring 服务(服务器)调用 GET 请求,该服务器正在接受 HTTP2 请求,我收到 302 作为响应。如何使用 spring RestTemplate 发送 Http2 请求。

Exception in thread "main" org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://example.com/static/mobile/get-token": unexpected end of stream on Connection{domain.com:443, proxy=DIRECT hostAddress=example.com cipherSuite=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 protocol=http/1.1}; nested exception is java.io.IOException: unexpected end of stream on Connection{example.com:443, proxy=DIRECT hostAddress=example.com cipherSuite=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 protocol=http/1.1}
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:732)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:600)
at com.domain.Sample.main(Sample.java:45)
Caused by: java.io.IOException: unexpected end of stream on Connection{example.com:443, proxy=DIRECT hostAddress=example.com cipherSuite=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 protocol=http/1.1}
    at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:205)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
    at okhttp3.RealCall.execute(RealCall.java:77)
    at org.springframework.http.client.OkHttp3ClientHttpRequest.executeInternal(OkHttp3ClientHttpRequest.java:73)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:723)
    ... 3 more

最佳答案

使用 OkHttp3ClientHttpRequestFactory .

RestTemplate http2Template = new RestTemplate(new OkHttp3ClientHttpRequestFactory());

关于spring - 如何配置 spring 的 RestTemplate 来发出 http2 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52222992/

相关文章:

java - 允许斜杠的 JAX-RS @Path 与其他资源发生冲突

java - Spring Controller 被调用但返回 404

spring-boot - 如何在使用 spring-boot 断路器(Hystrix)时将数据从业务方法传递到回退方法?

spring-boot - 使用 webclient webflux 将文件发布到接受 APPLICATION_OCTET_STREAM 的 API

java - 如何使用 Hibernate 和 Spring Boot 启用批量插入

angularjs - Angular HTML5 URLs - 服务器配置

spring - 扩展 RepositoryRestExceptionHandler

node.js - Node.js child_process.exec “/bin/sh: 1: docker: not found”错误

java - Spring MVC - 从另一个休息服务内部调用休息服务

event-handling - 为什么在基于编排的 Sagas 中命令是必要的?