admin管理员组

文章数量:1392116

I am using Spring's Http Interface to make HTTP requests through RestClient. Initially, I created a RestClient instance without setting a requestFactory, as shown below:

@Bean("rest-client-builder")
public RestClient.Builder globalRestClientBuilder() {
    return RestClient.builder();
}

@Bean("device-rest-client")
RestClient restClient(@Qualifier("rest-client-builder") RestClient.Builder builder) {
    return builder.baseUrl("http://127.0.0.1:8585").build();
}

@Bean(name = "deviceCredentialsRestClient")
DeviceCredentialsRestInterface provideDeviceCredentialsRestClient(
        @Qualifier("device-rest-client") RestClient restClient
) {
    RestClientAdapter adapter = RestClientAdapter.create(restClient);
    HttpServiceProxyFactory factory = HttpServiceProxyFactory.builderFor(adapter).build();
    return factory.createClient(DeviceCredentialsRestInterface.class);
}

With this configuration, when I make a request, I receive a 404 [no body] error with the following exception:

.springframework.web.client.HttpClientErrorException$NotFound: 404 : [no body]
    at .springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:112)
    at .springframework.web.client.StatusHandler.lambda$defaultHandler$3(StatusHandler.java:86)
    at .springframework.web.client.StatusHandler.handle(StatusHandler.java:146)
    at .springframework.web.client.DefaultRestClient$DefaultResponseSpec.applyStatusHandlers(DefaultRestClient.java:826)
    at .springframework.web.client.DefaultRestClient$DefaultResponseSpec.lambda$readBody$4(DefaultRestClient.java:815)
    at .springframework.web.client.DefaultRestClient.readWithMessageConverters(DefaultRestClient.java:215)
    at .springframework.web.client.DefaultRestClient$DefaultResponseSpec.readBody(DefaultRestClient.java:814)
    at .springframework.web.client.DefaultRestClient$DefaultResponseSpec.lambda$toEntityInternal$2(DefaultRestClient.java:770)
    at .springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.exchangeInternal(DefaultRestClient.java:574)
    at .springframework.web.client.DefaultRestClient$DefaultRequestBodyUriSpec.exchange(DefaultRestClient.java:535)
    at .springframework.web.client.RestClient$RequestHeadersSpec.exchange(RestClient.java:677)
    at .springframework.web.client.DefaultRestClient$DefaultResponseSpec.executeAndExtract(DefaultRestClient.java:809)
    at .springframework.web.client.DefaultRestClient$DefaultResponseSpec.toEntityInternal(DefaultRestClient.java:769)
    at .springframework.web.client.DefaultRestClient$DefaultResponseSpec.toEntity(DefaultRestClient.java:765)
    at .springframework.web.client.support.RestClientAdapter.exchangeForEntity(RestClientAdapter.java:85)
    at .springframework.web.service.invoker.HttpServiceMethod$ExchangeResponseFunction.lambda$create$3(HttpServiceMethod.java:418)
    at .springframework.web.service.invoker.HttpServiceMethod$ExchangeResponseFunction.execute(HttpServiceMethod.java:382)
    at .springframework.web.service.invoker.HttpServiceMethod.invoke(HttpServiceMethod.java:133)
    at .springframework.web.service.invoker.HttpServiceProxyFactory$HttpServiceMethodInterceptor.invoke(HttpServiceProxyFactory.java:243)
    at .springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
    at .springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:223)
    at jdk.proxy2/jdk.proxy2.$Proxy184.getDeviceCredentialsDetails(Unknown Source)
    at ir.co.isc.hamoon.papyrus.controller.IssuerPdfController.uploadOriginalPdf(IssuerPdfController.java:51)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    ...
    at ir.co.isc.hamoonmon.core.logging.SystemLogger.logMethodExecution(SystemLogger.java:42)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
    ...

Upon debugging, I noticed that the underlying clientRequestFactory is HttpComponentsClientHttpRequestFactory:

To resolve this, I modified the RestClient configuration to explicitly set the requestFactory to SimpleClientHttpRequestFactory, like this:

@Bean("rest-client-builder")
public RestClient.Builder globalRestClientBuilder() {
    return RestClient.builder().requestFactory(new SimpleClientHttpRequestFactory());
}

After this change, the HTTP request works successfully.

Question:

Can anyone explain why the first configuration (without setting the requestFactory) results in a 404 error, while setting the requestFactory to SimpleClientHttpRequestFactory resolves the issue? What is the difference between HttpComponentsClientHttpRequestFactory and SimpleClientHttpRequestFactory in this context?

本文标签: javaWhy does setting SimpleClientHttpRequestFactory resolve 404 errors in Spring RestClientStack Overflow