13

I have a spring application which requires to call REST based external API calls for some data.

The data format from the API is JSON.

My question is which one of the following options is better and light weight to make the external api calls

  1. Spring integration (using ws:outbound-gateway)

  2. Apache Commons HttpClient

Please share your thoughts...

skaffman
  • 398,947
  • 96
  • 818
  • 769
johnny
  • 141
  • 1
  • 2
  • 4

3 Answers3

16

As the others have mentioned both Spring RestTemplate and Jersey Rest Client will do the job. I have used both. Both them work great with Jackson and IIRC they will automatically use it if found (spring for sure).

There is one advantage I like about Spring RestTemplate is that you can plugin Commons HTTP as the transport. So if you had some weird headers, cookies, timeout, threading you can configure Commons HTTP and then put it into the RestTemplate.

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
CommonsClientHttpRequestFactory f = new CommonsClientHttpRequestFactory();
f.setReadTimeout(120 * 1000);

The point is if you are thinking about using Commons HTTP Client then as @Skaffman says RestTemplate is a no-brainer over something more complicated!

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • Sounds Great. So, with Spring Rest Template, can i achieve the following? 1) Make a HTTP call to http://xyz.com 2) Get the json response 3) Parse the json response to java objects – johnny Jul 27 '11 at 20:45
  • Yes and it will even do it automatically for you if you plugin a MessageConverter. See the javadoc for it. – Adam Gent Jul 27 '11 at 20:49
  • I had the wrong message converter in the example code before (jaxb) its now with jackson. – Adam Gent Jul 27 '11 at 20:51
  • Gotcha. Thanks for the details. From your details, Spring Rest template sounds a fit for my application. First, i shall go thru Spring Rest Template javadoc and understand the implementation details. – johnny Jul 27 '11 at 20:56
  • 1
    Thank you @Adam Gent. The suggested implementation worked for me and gives decent flexibility to modify my HTTPClient call settings. Is there a way in spring to measure the HTTP Calls performance (latency) ?? – johnny Aug 01 '11 at 17:26
  • @AdamGent I have asked question on `RestTemplate` [here](http://stackoverflow.com/questions/27770928/apache-httpclient-vs-resttemplate-way-to-execute-an-url). If possible, can you help me out there? I am little bit confuse why same stuff doesn't work with RestTemplate. – john Jan 04 '15 at 22:46
10

Spring comes with a class called RestTemplate (javadoc) that should make this sort of thing easy. It hides the HTTP handling and provides a REST-style operations interface. It includes support for message converters for converting to and from JSON (in this case, Spring has support for the Jackson library).

Spring Integration is huge overkill for this - REST is inherently simple. Commons HttpClient would work, but leaves you with extra work to do on top of that.

See the section of the Spring docs on how to use RestTemplate, and the JSON message conversion.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Could you please provide more information, why spring integration is a over kill? – johnny Jul 27 '11 at 20:37
  • @johnny: Because it's a big complex library intended for integrating with legacy systems. `RestTemplate` is just one class, it's easy to use, and it's already part of Spring. Sounds like a no-brainer to me. – skaffman Jul 27 '11 at 20:39
  • @johnny see my answer... you can plugin Commons Http into Spring RestTemplate. – Adam Gent Jul 27 '11 at 20:45
3

I have used Spring & Jersey. Jersey makes it easy to build RESTful Web services with Spring through the use of annotations like @GET&@POST & @PUT @DELETE bundle with JAX-RS library.

KnownSubset
  • 139
  • 7
  • All my API requests are simple Get requests with data coming back in JSON. My intial idea was use use Apache HTTP client to make the API call and use jackson to parse the JSON response. I started reading about spring integration and wanted to know, if any one is using spring integration for this purpose... – johnny Jul 27 '11 at 20:16
  • You are able to do simple get requests by doing the following with Jersy for JSON: `String BASE_URI = "http://localhost:8080/HelloWorldWebapp/resources";` `Client client = Client.create();` `WebResource webResource = client.resource(BASE_URI);` `String jsonData = webResource.path("foo/").accept("application/json").get(String.class);` – KnownSubset Jul 29 '11 at 02:23