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的数据的解析
}