Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient vs Spring Rest Template?

What's the best way to make a REST call?

Should I use Apache Http Client or Should I use Spring Rest Template.

On what basis I can decide which one I should go for?

I need to make a call to this url-

http://localhost:8080/service/Service/v1/get/USERID=10000/profile.ACCOUNT.SERVICE"

And after getting the response back, I just need to see whether that response contains any particular string or not.

like image 741
AKIWEB Avatar asked Feb 14 '13 03:02

AKIWEB


People also ask

Does Spring RestTemplate use Apache HTTP client?

Spring RestTemplate Configuration HttpComponentsClientHttpRequestFactory is ClientHttpRequestFactory implementation that uses Apache HttpComponents HttpClient to create requests.

What is the difference between rest template and Web client?

RestTemplate uses Java Servlet API and is therefore synchronous and blocking. Conversely, WebClient is asynchronous and will not block the executing thread while waiting for the response to come back. The notification will be produced only when the response is ready.

Which is better RestTemplate or WebClient?

Compared to RestTemplate , WebClient has a more functional feel and is fully reactive. Since Spring 5.0, RestTemplate is deprecated. It will probably stay for some more time but will not have major new features added going forward in future releases. So it's not advised to use RestTemplate in new code.

Why RestTemplate is deprecated?

RestTemplate provides a synchronous way of consuming Rest services, which means it will block the thread until it receives a response. RestTemplate is deprecated since Spring 5 which means it's not really that future proof.


1 Answers

Spring RestTemplate follows the pattern for all the *Template classes within the core Spring framework and the various sub-frameworks: JdbcTemplate, HibernateTemplate, WebServiceTemplate etc etc.

The idea of all of these Template classes is to reduce the boilerplate code (exception handling, repetitive stuff and concentrate on your business logic). I would definitely use it over the simple HttpClient.

To get the class you'll need the spring-web dependency.

like image 187
abalogh Avatar answered Oct 27 '22 01:10

abalogh