Spring Integration or Apache HTTP Client

喜欢而已 提交于 2019-12-30 04:03:12

问题


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...


回答1:


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!




回答2:


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.




回答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.



来源:https://stackoverflow.com/questions/6850344/spring-integration-or-apache-http-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!