Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

setConnectionTimeout on RestTemplateBuilder is not Working #954

Closed
goafabric opened this issue Aug 6, 2021 · 4 comments · Fixed by #959
Closed

setConnectionTimeout on RestTemplateBuilder is not Working #954

goafabric opened this issue Aug 6, 2021 · 4 comments · Fixed by #959
Labels
type: compatibility Native image compatibility issue
Milestone

Comments

@goafabric
Copy link

Imagine the following code

final RestTemplate restTemplate = new RestTemplateBuilder()
.setConnectTimeout(Duration.ofMillis(timeout))
.setReadTimeout(Duration.ofMillis(timeout))
.build();

While working in normal mode, a call inside a Native Image will crash with:
"SimpleClientHttpRequestFactory does not have a suitable setConnectTimeout method"

Would be nice if this can be fixed.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 6, 2021
@ttddyy
Copy link
Contributor

ttddyy commented Aug 7, 2021

Hi,

I just checked why.

The RestTemplateBuilder uses reflection to access setConnectTimeout/setReadTimeout/setBufferRequestBody methods on underlying ClientHttpRequestFactory.

https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/client/RestTemplateBuilder.java#L760-L784

So those methods need some entries for @TypeHint/reflect-config.json.

@goafabric
Copy link
Author

Yes you are right .. wouldn't have thought that this is so easy
A simple
@typehint(types = org.springframework.http.client.SimpleClientHttpRequestFactory.class)

fixes the issue ... would be nice though if RestTemplateBuilder will have this out of the box ..
Thx 4 the quick support.

One more thing .. do you know why all returned classes from a RestTemplate call
also have to be registered for Reflection manually ?
I guess because the JSON (De)Serializer creates them via Reflection ?

Quarkus currently does this automatically for everything annotated with "RegisterRestCLient"

ttddyy added a commit to ttddyy/spring-native that referenced this issue Aug 8, 2021
The `connectTimeout` `readTimeout` and `bufferRequestBody` methods on
`RestTemplateBuilder` uses reflection to access underlying
`ClientHttpRequestFactory` implementation class.

Currently, type hints for `HttpComponentsClientHttpRequestFactory` and
`OkHttp3ClientHttpRequestFactory` exist, but the fallback request
factory, `SimpleClientHttpRequestFactory` hint doesn't exist.
Therefore, when `RestTemplate` doesn't detect either commons-client or
okhttp, it falls back to the `SimpleClientHttpRequestFactory` and the
reflective access fails to find the class.

This PR simply adds a hint to expose `SimpleClientHttpRequestFactory`,
so that above reflective methods on `RestTemplateBuilder` will have
access to it.

Fixes spring-atticgh-954
@ttddyy
Copy link
Contributor

ttddyy commented Aug 8, 2021

I have created a PR to add a type hint for SimpleClientHttpRequestFactory.
#959

One more thing .. do you know why all returned classes from a RestTemplate call
also have to be registered for Reflection manually ?
I guess because the JSON (De)Serializer creates them via Reflection ?

Yeah, I think so.
To map the json string to the target type object, jackson should be constructing the target type dynamically unless custom (de)serializers are used.

I think one way to achieve it would be spring-aot may look for the return types from controllers and automatically register them.
Or, spring-aot may generate serializers that would convert to the target type explicitly without reflection. Just my ideas.

@goafabric
Copy link
Author

Thank you

@sdeleuze sdeleuze added this to the 0.10.3 milestone Aug 15, 2021
@sdeleuze sdeleuze added type: compatibility Native image compatibility issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Aug 15, 2021
sdeleuze pushed a commit that referenced this issue Aug 17, 2021
The `connectTimeout` `readTimeout` and `bufferRequestBody` methods on
`RestTemplateBuilder` uses reflection to access underlying
`ClientHttpRequestFactory` implementation class.

Currently, type hints for `HttpComponentsClientHttpRequestFactory` and
`OkHttp3ClientHttpRequestFactory` exist, but the fallback request
factory, `SimpleClientHttpRequestFactory` hint doesn't exist.
Therefore, when `RestTemplate` doesn't detect either commons-client or
okhttp, it falls back to the `SimpleClientHttpRequestFactory` and the
reflective access fails to find the class.

This PR simply adds a hint to expose `SimpleClientHttpRequestFactory`,
so that above reflective methods on `RestTemplateBuilder` will have
access to it.

Fixes gh-954
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: compatibility Native image compatibility issue
Development

Successfully merging a pull request may close this issue.

4 participants