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
Given that the
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
REST Client Code
REST Client Code
REST Client Code
Let me know if something needs more explanation.
Happy Learning !!
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
RestTemplateclass 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
RestTemplateclass 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.
HTTP GET Method Example
1) Get XML representation of employees collection in String formatREST API Code
REST API Code
REST API Code
REST API Code
REST API Code
HTTP POST Method Example
REST API CodeHTTP PUT Method Example
REST API CodeHTTP DELETE Method Example
REST API CodeHappy Learning !!
相关文章推荐
- Java Exlips 操作 Hadoop
- java一些注解
- Java环境变量批处理文件
- Java并发编程:Callable、Future和FutureTask
- java函数参数类型后添加三点的用法
- JAVASE学习笔记:第五章 JAVA面向对象(三)
- 解决android开发中eclipse不能自动提示
- aop学习总结一------使用jdk动态代理简单实现aop功能
- Java中的核心技术思想
- JAVASE学习笔记:第五章 JAVA面向对象(二)
- Spring3自定义环境配置 <beans profile="">
- java基础(九)---继续聊聊map
- SpringMVC 学习笔记(三) 使用ServletAPI 和 实体 做为参数
- JAVASE学习笔记:第四章 JAVA数组
- json和map以及javabean之间的转换
- java web项目的部署
- java web项目的部署
- 【JAVA基础教程】-浅谈数组及其内存控制
- 团队开发中,eclipse中安装jre
- JAVASE学习笔记:第三章 JAVA流程控制语句