我们正在使用RestEasy客户端版本3.0 * 我们正在尝试在请求正文中发布非常大的内容 当内容长度小于1 MB时,以下代码正确运行 当内容长度非常大(约500 MB)时,请求就会卡住 代码看起来像这样:
InputStream inputStream = new FileInputStream(tempFile);
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target("<SomeUrl>");
response = target.request(MediaType.WILDCARD).post(Entity.entity(inputStream, MediaType.WILDCARD));
我应该如何更改代码以支持大型内容?
一周后......
由于没有人回答我 - 我使用了古老的Spring解决方案(见下文)
Spring比RestEasy客户端好吗?
InputStream inputStream = new FileInputStream(tempFile);
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setBufferRequestBody(false);
RestTemplate r = new RestTemplate(factory);
RequestCallback requestCallback = new RequestCallback() {
@Override
public void doWithRequest(ClientHttpRequest clientHttpRequest) throws IOException {
List<org.springframework.http.MediaType> MEDIA_TYPES = Collections.unmodifiableList(Arrays.asList(org.springframework.http.MediaType.ALL));
clientHttpRequest.getHeaders().setAccept(MEDIA_TYPES);
OutputStream requestOutputStream = clientHttpRequest.getBody();
try {
int copiedBytes = IOUtils.copy(inputStream, requestOutputStream);
} finally {
IOUtils.closeQuietly(requestOutputStream);
}
}
};
ResponseExtractor<Response> responseExtractor = new ResponseExtractor<Response>() {
@Override
public Response extractData(ClientHttpResponse response) throws IOException {
return Response.status(response.getRawStatusCode()).build();
}
};
response = r.execute(url, HttpMethod.POST, requestCallback, responseExtractor);
答案 0 :(得分:0)
我想在Spring 4.0.1中你可以使用Apache HTTP客户端和HttpComponentsClientHttpRequestFactory而不是SimpleClientHttpRequestFactory。以下是原始备注:SPR-10728
<bean id="restTemplateStreaming" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
<bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<property name="bufferRequestBody" value="false" />
<property name="httpClient">
<bean class="com.spotfire.server.http.spring.HttpClientFactory">
<property name="clientBuilder">
<bean class="org.apache.http.impl.client.HttpClientBuilder">
<property name="connectionManager" ref="poolingHttpClientConnectionManager" />
<property name="redirectStrategy">
<bean class="org.apache.http.impl.client.LaxRedirectStrategy" />
</property>
</bean>
</property>
</bean>
</property>
</bean>
</constructor-arg>
</bean>
注意 但是回调实现也会发生变化。你不能再调用clientHttpRequest.getBody(); 相反,你必须使用一个有点复杂的结构,如下所示:
class StreamingRequestCallback implements RequestCallback {
private final InputStream inputStream;
StreamingRequestCallback(InputStream inputStream) {
this.inputStream = inputStream;
}
@Override
public void doWithRequest(final ClientHttpRequest request) throws IOException {
request.getHeaders().add("Content-type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
((StreamingHttpOutputMessage) request).setBody(new StreamingHttpOutputMessage.Body() {
@Override
public void writeTo(final OutputStream outputStream) throws IOException {
IOUtils.copy(inputStream, outputStream);
}
});
}
};