RestTemplate的用法

1.使用Spring的RestTemplate来获取Facebook的Graph API的数据。(符合Restful风格)

http://graph.facebook.com/pivotalsoftware
这个url 将返回一大堆Json串。 它将返回的JSON字符串为:它将返回的JSON字符串为:
{
  "id": "161112704050757",
  "about": "Pivotal is enabling the creation of modern software applications that leverage big & fast data \u2013 on a single, cloud independent platform.",
  "can_post": false,
  "category": "Internet/software",
  "category_list": [
    {
      "id": "108472109230615",
      "name": "Computer Services"
    }
  ],
  "checkins": 42,
  "cover": {
    "cover_id": 163344023827625,
    "offset_x": 0,
    "offset_y": 0,
    "source": "https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-xaf1/t1.0-9/s720x720/554668_163344023827625_839302172_n.png"
  },
  "description": "Pivotal, the company at the intersection of big data, PaaS, and agile development, helps companies transform into great software companies. It starts with Pivotal One, a comprehensive solution that includes a set of application and data services that run on top of Pivotal CF, a turnkey platform-as-a-service (PaaS) solution for agile development teams to rapidly update and scale applications on a private cloud that can be instantly expanded and upgraded with no downtime, allowing enterprises to innovate with disruptive speed.\n\nREVOLUTIONARY COMPREHENSIVE PAAS\nPivotal One is a next generation PaaS that can be deployed on multiple cloud environments to deliver a turnkey experience for scaling and updating PaaS with no downtime. Pivotal One Services helps create a PaaS that no other vendor can offer in the industry by integrating differentiated data services such as Hadoop and visual analytics.\n\nSPEED TIME TO MARKET\nDriving the demand for PaaS is the trend of software being the competitive edge across all industries. This trend has unleashed a new generation of developers driving a deep shift in platforms and processes. These developers work in agile teams and demand a platform that allows them to continuously deliver updates to and horizontally scale their applications with no downtime. They seek standardized ways to plug in leading data services and perform deep user analytics on top of massive datasets to drive rapid iteration based on customer needs.\n\nABOUT PIVOTAL\nPivotal, committed to open source and open standards, is a leading provider of application and data infrastructure software, agile development services, and data science consulting. Follow Pivotal on Twitter \u0040gopivotal.",
  "founded": "2013",
  "has_added_app": false,
  "is_community_page": false,
  "is_published": true,
  "likes": 1022,
  "link": "https://www.facebook.com/pivotalsoftware",
  "location": {
    "city": "San Francisco",
    "country": "United States",
    "latitude": 37.78199,
    "longitude": -122.40406,
    "state": "CA",
    "street": "875 Howard St",
    "zip": "94103"
  },
  "mission": "Pivotal, the company at the intersection of big data, PaaS, and agile development, helps companies transform into great software companies. ",
  "name": "Pivotal",
  "parking": {
    "lot": 0,
    "street": 0,
    "valet": 0
  },
  "phone": "(650) 286-8012",
  "products": "PaaS:\nPivotal One, Pivotal CF, Cloud Foundry\n\nDATA: Pivotal HD, Pivotal HD with GemFire XD, Pivotal Greenplum DB, Pivotal Data Dispatch,  Pivotal GemFire, Pivotal SQLFire, Redis\nPaaS: Pivotal One, Pivotal CF, Pivotal Web Services, Cloud Foundry\nDATA TOOLS: Pivotal VRP, Pivotal Command Center\nANALYTICS: MADlib, Pivotal GPText\nAPPLICATIONS: Pivotal tc Server, Pivotal Web Server, Pivotal RabbitMQ, Spring, vFabric Suite",
  "talking_about_count": 77,
  "username": "pivotalsoftware",
  "website": "http://www.gopivotal.com",
  "were_here_count": 42
}



2。接下来我们需要使用RestTemplate来解析这对json串。首先创建一个POJO来封装这堆数据。

package jlong;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true) //这里使用了@JsonIgnoreProperties是来自Jackson JSON包,指示忽略不在范围内的所有属性。即:RestTemplate在将这堆json转换成你定义的对象的时候。只关注你定义的对象里面的属性。
public class Facebook {


  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;
  }


}

核心代码:

package jlong;


import org.springframework.web.client.RestTemplate;


public class Test{


  public static void main(String args[]) {
    RestTemplate restTemplate = new RestTemplate();
    Facebook  fb= restTemplate.getForObject("http://graph.facebook.com/pivotalsoftware", Facebook.class);
    System.out.println("Name: " + fb.getName());
    System.out.println("About:   " + fb.getAbout());
    System.out.println("Phone:   " + fb.getPhone());
    System.out.println("Website: " + fb.getWebsite());
  }

以上即可完成使用RestTemplate对Restful风格的url的数据的解析
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值