public RestTemplate build() {
return this.build(RestTemplate.class);
}
public <T extends RestTemplate> T build(Class<T> restTemplateClass) {
return this.configure((RestTemplate)BeanUtils.instantiateClass(restTemplateClass));
}
I find above code in RestTemplateBuilder
,and this confused me。
Why use reflection to construct a RestTemplate object ?
What's wrong with doing something like this?
// public RestTemplate build() {
// return this.build(RestTemplate.class);
// }
public RestTemplate build() {
return this.build(new RestTemplate());
}
// Use an object directly as a parameter other than a "Class"
public <T extends RestTemplate> T build(T restTemplate) {
return this.configure(restTemplate);
}
public <T extends RestTemplate> T build(Class<T> restTemplateClass) {
return this.configure(BeanUtils.instantiateClass(restTemplateClass));
}
public <T extends RestTemplate> T configure(T restTemplate) {
// .....
return restTemplate;
}
Comment From: bclozel
The RestTemplateBuilder
class belongs to the spring-boot project, Spring Framework only ships RestTemplate
itself. I believe this method was added to enable other implementations that extend RestTemplate
(like the now deprecated OAuth2RestTemplate
in Spring Security). Spring Boot might reconsider this method in a future major version.
We tend to use Stack Overflow for questions. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements.
Thanks!