码迷,mamicode.com
首页 > Web开发 > 详细

Consuming a RESTful Web Service

时间:2015-03-03 11:30:27      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

创建项目步骤不做描述了直奔主题

1.修改pom.xml 文件

 

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-release</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-release</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>

 

2.添加实体类

 

@JsonIgnoreProperties(ignoreUnknown = true)
public class Greeting {

private String id;
private String content;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

}

 

3.添加测试类

public class Application {

public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
Greeting page = restTemplate.getForObject("http://localhost:8080/greeting", Greeting.class);
System.out.println("id:" + page.getId());
System.out.println("content:" + page.getContent());
}

}

 

4.测试打印成功:

id:0
content:hello,world!

 

总结:将restful系统返回的内容转换成普通的java对象

 

Consuming a RESTful Web Service

标签:

原文地址:http://www.cnblogs.com/summer-arvin/p/4310452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!