简体   繁体   English

Spring集成或Apache HTTP客户端

[英]Spring Integration or Apache HTTP Client

I have a spring application which requires to call REST based external API calls for some data. 我有一个spring应用程序,它需要为某些数据调用基于REST的外部API调用。

The data format from the API is JSON. API的数据格式是JSON。

My question is which one of the following options is better and light weight to make the external api calls 我的问题是,以下哪个选项更好,重量更轻,可以进行外部api调用

  1. Spring integration (using ws:outbound-gateway) Spring集成(使用ws:outbound-gateway)

  2. Apache Commons HttpClient Apache Commons HttpClient

Please share your thoughts... 请分享你的想法......

As the others have mentioned both Spring RestTemplate and Jersey Rest Client will do the job. 正如其他人提到的那样, Spring RestTemplateJersey Rest Client都可以完成这项工作。 I have used both. 我用过这两个。 Both them work great with Jackson and IIRC they will automatically use it if found (spring for sure). 他们都与杰克逊和IIRC合作很好,如果找到他们会自动使用它(肯定是春天)。

There is one advantage I like about Spring RestTemplate is that you can plugin Commons HTTP as the transport. 我喜欢Spring RestTemplate的一个优点是你可以将Commons HTTP作为传输插件。 So if you had some weird headers, cookies, timeout, threading you can configure Commons HTTP and then put it into the RestTemplate. 因此,如果您有一些奇怪的标题,cookie,超时,线程,您可以配置Commons HTTP然后将其放入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! 关键是如果你正在考虑使用Commons HTTP Client,那么@Skaffman说RestTemplate对于更复杂的事情是一个明智的选择!

Spring comes with a class called RestTemplate ( javadoc ) that should make this sort of thing easy. Spring附带了一个名为RestTemplatejavadoc )的类,它可以使这类事情变得简单。 It hides the HTTP handling and provides a REST-style operations interface. 它隐藏了HTTP处理并提供了REST样式的操作界面。 It includes support for message converters for converting to and from JSON (in this case, Spring has support for the Jackson library). 它包括对用于转换到JSON和从JSON转换的消息转换器的支持(在这种情况下,Spring支持Jackson库)。

Spring Integration is huge overkill for this - REST is inherently simple. Spring Integration对此非常过分 - REST本质上很简单。 Commons HttpClient would work, but leaves you with extra work to do on top of that. Commons HttpClient可以工作,但除此之外还有额外的工作要做。

See the section of the Spring docs on how to use RestTemplate , and the JSON message conversion. 请参阅Spring文档中有关如何使用RestTemplate和JSON消息转换的部分。

I have used Spring & Jersey . 我用过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. 新泽西可以很容易地通过使用类似的注释建立与Spring RESTful Web服务的@GET@POST@PUT @DELETE与JAX-RS库捆绑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM