java HttpClient 与 Spring Rest 模板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14867199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:40:45  来源:igfitidea点击:

HttpClient vs Spring Rest Template?

javaspringresthttpclient

提问by AKIWEB

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

进行 REST 调用的最佳方式是什么?

Should I use Apache Http Clientor Should I use Spring Rest Template.

我应该使用Apache Http Client还是应该使用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"

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.

在得到响应后,我只需要查看该响应是否包含任何特定字符串。

回答by abalogh

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

春天RestTemplate遵循核心Spring框架内的所有*模板类模式和各个子框架:JdbcTemplateHibernateTemplateWebServiceTemplate等等等等。

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.

所有这些模板类的想法是减少样板代码(异常处理、重复的东西并专注于您的业务逻辑)。我肯定会在简单的HttpClient.

To get the class you'll need the spring-webdependency.

要获得该类,您将需要spring-web依赖项。