博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Consuming a RESTful Web Service
阅读量:7072 次
发布时间:2019-06-28

本文共 2899 字,大约阅读时间需要 9 分钟。

  本篇文章将介绍使用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就可以使用 将JSON对象映射为我们定义的model。

  虽然在这快我们使用的是get请求,但是RestTemplate也支持POST,PUT,DELETE请求。

  最后运行我们的程序,数据结果如下:

  

Name:    PivotalAbout:   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-8012Website: http://www.gopivotal.com

转载地址:http://wozml.baihongyu.com/

你可能感兴趣的文章
github上的项目发布成静态网页
查看>>
服务器宕机问题
查看>>
关于PCA和SVD的认识
查看>>
PHP中利用PCLZIP压缩解压文件
查看>>
HDU小小练
查看>>
flex控件例子
查看>>
获取定位信息
查看>>
数据结构与算法入门
查看>>
crt 和 Windows之间传输大文件
查看>>
软件项目版本号的命名规则及格式
查看>>
Jetty
查看>>
ARC
查看>>
IntelliJ IDEA java项目导入jar包,打jar包
查看>>
理解与应用css中的display属性
查看>>
78.员工个人信息保镖页面 Extjs 页面
查看>>
异常:Project configuration is not up-to-date with pom.xml解决方案
查看>>
Apple 微软 Google 都在竞购一家创业公司
查看>>
空合并运算符“??”
查看>>
Codeforces Round 428 B Game of the rows 贪心 思维
查看>>
asp.net三层架构初探<二>
查看>>