Consuming a RESTful Web Service
本篇文章将介绍使用Spring来建立RESTful的Web Service。
我们通过一个例子来说明这篇文章:这个例子将会使用Spring的RestTemplate来从Facebook的提供的API中获取一些信息。然后对这些信息进行一些处理。Facebook的API为:
http://graph.facebook.com/gopivotal
其实在这个例子中,这个API只是为了掩饰用,并没有特别的含义。这个例子也只是为了说明从一个在线的接口中获取一些数据并进行处理。
当我们通过浏览器或者curl请求这个路径的时候会返回数据格式为:
{
"id": "161112704050757",
"about": "At Pivotal, our mission is to enable customers to build a new class of applications, leveraging big and fast data, and do all of this with the power of cloud independence. ",
"app_id": "0",
"can_post": false,
"category": "Internet/software",
"checkins": 0,
"cover": {
"cover_id": 163344023827625,
"source": "http://sphotos-d.ak.fbcdn.net/hphotos-ak-frc1/s720x720/554668_163344023827625_839302172_n.png",
"offset_y": 0,
"offset_x": 0
},
"founded": "2013",
"has_added_app": false,
"is_community_page": false,
"is_published": true,
"likes": 126,
"link": "https://www.facebook.com/gopivotal",
"location": {
"street": "1900 South Norfolk St.",
"city": "San Mateo",
"state": "CA",
"country": "United States",
"zip": "94403",
"latitude": 37.552261,
"longitude": -122.292152
},
"name": "Pivotal",
"phone": "650-286-8012",
"talking_about_count": 15,
"username": "gopivotal",
"website": "http://www.gopivotal.com",
"were_here_count": 0
}
的数据。但是我们只需要其中的一些很少的信息。在这种情况下我们就可以使用Spring的RestTemplate来帮助我们完成这个工作:
我们通过一个Model来定义我们需要的一些属性:
package hello; import org.codehaus.jackson.annotate.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown=true)
public class Page { private String name;
private String about;
private String phone;
private String website; public String getName() {
return name;
} public String getAbout() {
return about;
} public String getPhone() {
return phone;
} public String getWebsite() {
return website;
} }
使用@JsonIgnoreProperties注解来忽略一些我们我们不需要的属性。
然后我们就可以编写下面的方法来完成我们的工作:
package hello;
import org.springframework.web.client.RestTemplate;
public class Application {
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
Page page = restTemplate.getForObject("http://graph.facebook.com/gopivotal", Page.class);
System.out.println("Name: " + page.getName());
System.out.println("About: " + page.getAbout());
System.out.println("Phone: " + page.getPhone());
System.out.println("Website: " + page.getWebsite());
}
}
因为我们在classpath中增加了 Jackson 库,所以spring就可以使用 message converter将JSON对象映射为我们定义的model。
虽然在这快我们使用的是get请求,但是RestTemplate也支持POST,PUT,DELETE请求。
最后运行我们的程序,数据结果如下:
Name: Pivotal
About: At Pivotal, our mission is to enable customers to build a new class of applications, leveraging big and fast data, and do all of this with the power of cloud independence.
Phone: 650-286-8012
Website: http://www.gopivotal.com
Consuming a RESTful Web Service的更多相关文章
- 译:3.消费一个RESTful Web Service
这节课我们根据官网教程学习如何去消费(调用)一个 RESTful Web Service . 原文链接 https://spring.io/guides/gs/consuming-rest/ 本指南将 ...
- 【转】基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)
转载:http://www.cnblogs.com/windwithlife/archive/2013/03/03/2942157.html 一,选择一个合适的,Web开发环境: 我选择的是Eclip ...
- 【转】 Build a RESTful Web service using Jersey and Apache Tomcat 2009
Build a RESTful Web service using Jersey and Apache Tomcat Yi Ming Huang with Dong Fei Wu, Qing GuoP ...
- Building a RESTful Web Service Using Spring Boot In Eclipse
一.构建restful web service 创建Maven的java web工程,maven的pom文件加入依赖包 创建包hello Greeting.java package hello; pu ...
- 使用Java创建RESTful Web Service
REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移).2000年Roy Fielding博士在他的博士论文“Architectural Sty ...
- (转)接口自动化测试 – Java+TestNG 测试 Restful Web Service
本文主要介绍如何用Java针对Restful web service 做接口自动化测试(数据驱动),相比UI自动化,接口自动化稳定性可靠性高,实施难易程度低,做自动化性价比高.所用到的工具或类库有 T ...
- Apache CXF实现Web Service(4)——Tomcat容器和Spring实现JAX-RS(RESTful) web service
准备 我们仍然使用 Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 中的代码作为基础,并 ...
- Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service
起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先 ...
- Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service
实现目标 http://localhost:9000/rs/roomservice 为入口, http://localhost:9000/rs/roomservice/room为房间列表, http: ...
随机推荐
- jboss:在standalone.xml中设置系统属性(system-properties)
就象在.net的web应用中,可以在web.config中设置appSettings一样,jboss的standalone.xml中也可以由开发人员自行添加系统属性,用法如下: </extens ...
- ICSharpCode.SharpZipLib
ICSharpCode.SharpZipLib 压缩.解压文件 附源码 http://www.icsharpcode.net/opensource/sharpziplib/ 有SharpZipli ...
- 新时代的coder如何成为专业程序员
在移动互联网"泛滥"的今天,越来越多非专业(这里的非专业指的是非计算机专业毕业的程序员)程序员加入到了IT行业中来了,可能是因为移动互联网的火爆导致程序员容易就业而且工资很高,可能 ...
- SSH框架 sequence diagram
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
- git标签
git标签 如果你达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以使用 git tag 给它打上标签.-a 选项意为"创建一个带注解的标签". 添加标签命令: $ gi ...
- 【BZOJ 3735】苹果树 树上莫队(树分块+离线莫队+鬼畜的压行)
2016-05-09 UPD:学习了新的DFS序列分块,然后发现这个东西是战术核导弹?反正比下面的树分块不知道要快到哪里去了 #include<cmath> #include<cst ...
- 【BZOJ 1758】【WC 2010】重建计划 分数规划+点分治+单调队列
一开始看到$\frac{\sum_{}}{\sum_{}}$就想到了01分数规划但最终还是看了题解 二分完后的点分治,只需要维护一个由之前处理过的子树得出的$tb数组$,然后根据遍历每个当前的子树上的 ...
- Edge Linking
因为噪声的存在, 检测出来的edge points有很多都是不相邻的. 所以边缘检测算法通常都有最后的连接步骤: 将属于同一edge的不相邻点连接起来(TODO, 是用一条路径将它们连通, 把路径中的 ...
- jQ插件--时间线插件和拖拽API
这个时间轴是工作上用到的,自己写了一个, qq空间有时间轴的控件, 百度文库也有时间轴的控件: 百度的时间轴大概是这样的: 用户点击对应的锚链接, 那个三角会滚动, 然后左侧的界面也会滚动: 实际的 ...