5
\$\begingroup\$

I am using RestTemplate to make an HTTP call to one of my service and I would like to have timeout for my HTTP Request:

public static String getResponse(final String url) {
 RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
 String response = restTemplate.getForObject(url, String.class);
 // return response
}
private ClientHttpRequestFactory clientHttpRequestFactory() {
 HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
 factory.setReadTimeout(2000);
 factory.setConnectTimeout(2000);
 return factory;
} 

(I came up with this after reading this.)

Is this an efficient way of using RestTemplate along with ClientHttpRequestFactory? Since with this for each call, it will make a new RestTemplate object. Earlier, I was not using any timeout for RestTemplate so I declared RestTemplate as static final.

private static final RestTemplate restTemplate = new RestTemplate();
public static String getResponse(final String url) {
 String response = restTemplate.getForObject(url, String.class);
 // return response
}

I am not sure what is the right way of using RestTemplate along with ClientHttpRequestFactory. Should they both be declared as static?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Sep 6, 2014 at 6:07
\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

In the example you linked, RestTemplate is annotated with @Bean:

@Bean
public RestTemplate restTemplate() {
 return new RestTemplate(clientHttpRequestFactory());
}

Beans are (normally) singletons in Spring, intended for reuse. This related post also confirms that RestTemplate is thread-safe, and it would be wasteful to recreate it every time.

It would be best to configure RestTemplate as in the linked article, and inject into your setup using Spring's annotations based configuration. Or, you could make it a private final member of your class.

class RequestHandler {
 private final RestTemplate restTemplate;
 public RequestHandler() {
 restTemplate = new RestTemplate(clientHttpRequestFactory());
 }
 public RestTemplate getRestTemplate() {
 return restTemplate;
 }
}
answered Sep 6, 2014 at 6:42
\$\endgroup\$
4
  • \$\begingroup\$ Thanks for explanation. I don't need to use inject in my code using Spring annotations. How can I normally use it as private final member in my class along with ClientHttpRequestFactory corresponding to my examplle? \$\endgroup\$ Commented Sep 6, 2014 at 6:58
  • \$\begingroup\$ I don't understand what you don't understand about private final member variables. I added an example just in case. \$\endgroup\$ Commented Sep 6, 2014 at 7:20
  • \$\begingroup\$ Thanks janos. What I meant was, I didn't wanted to use any bean annotations in my code. In your example, I should be having clientHttpRequestFactory() method in same RequestHandler class right? And also how will I get the instance of RestTemplate from RequestHandler class? \$\endgroup\$ Commented Sep 6, 2014 at 7:48
  • \$\begingroup\$ I meant that restTemplate should be a member in the same class as your getResponse method. Since you haven't shared your class structure, it's hard to be more specific. I believe your main question was if it's better to reuse a RestTemplate than recreate. Yes it is. When you say you don't want to use bean annotations in your code, I'm getting the feeling that maybe you don't know how to use spring correctly to configure your application. Maybe you're not enjoying all the benefits Spring can bring to you. But that discussion doesn't belong in this question. \$\endgroup\$ Commented Sep 6, 2014 at 8:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.