您的位置:首页 > 编程语言 > Java开发

Spring RESTFul Client – RestTemplate Example--转载

2015-06-04 15:41 597 查看
原文地址:http://howtodoinjava.com/2015/02/20/spring-restful-client-resttemplate-example/

After learning to build Spring REST based RESTFul APIs for XML representation and JSON representation, let’s build a RESTFul client to consume APIs which we have written. Accessing a third-party REST service inside a Spring application revolves around the use of the Spring RestTemplate class. The
RestTemplate
class is designed on the same principles as the many other Spring *Template classes (e.g.,
JdbcTemplate
,
JmsTemplate
), providing a simplified approach with default behaviors for performing complex tasks.

Given that the
RestTemplate
class is designed to call REST services, it should come as no surprise that its main methods are closely tied to REST’s underpinnings, which are the HTTP protocol’s methods: HEAD, GET, POST, PUT, DELETE, and OPTIONS. E.g. it’s methods are
headForHeaders()
,
getForObject()
,
postForObject()
,
put()
and
delete()
etc.


Read More and Source Code : Spring REST JSON Example


HTTP GET Method Example

1) Get XML representation of employees collection in String format

REST API Code

REST Client Code

2) Get JSON representation of employees collection in String format

REST API Code

REST Client Code

3) Using custom HTTP Headers with RestTemplate

REST API Code

REST Client Code

4) Get data as mapped object

REST API Code

REST Client Code

5) Passing parameters in URL

REST API Code

REST Client Code

HTTP POST Method Example

REST API Code

REST Client Code

HTTP PUT Method Example

REST API Code

REST Client Code

HTTP DELETE Method Example

REST API Code

REST Client Code

Let me know if something needs more explanation.

Happy Learning !!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: