- 浏览: 395753 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
tyjdzr:
程序已经运行成功,第一次接触webservice,楼主领我入门 ...
java实现webservice实例 -
guosongchao:
楼主介绍的确实简单,但是对于我这种对web service一点 ...
java实现webservice实例 -
gnail_oug:
...
java实现webservice实例 -
safedriver:
楼主真心不错
java实现webservice实例 -
Partys:
Partys 写道addArticleCategories(C ...
RESTful Web Services in Spring 3(上)
上一篇我主要发了RESTful Web Services in Spring 3的服务端代码,这里我准备写客户端的代码。
上篇得连接地址为:http://yangjizhong.iteye.com/blog/600540
开始本篇了:
注:附件里有源码,下载即可,依赖包请在spring网获得,谢谢。
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:oxm="http://www.springframework.org/schema/oxm" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"> <context:component-scan base-package="com.informit.articleservice" /> <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> <property name="messageConverters"> <list> <!-- We only have one message converter for the RestTemplate, namely the XStream Marshller --> <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> <constructor-arg> <bean class="org.springframework.oxm.xstream.XStreamMarshaller"> <!-- Tell XStream to find the alias names in the following classes --> <property name="annotatedClasses"> <list> <value>com.informit.articleservice.model.Article</value> <value>com.informit.articleservice.model.Category</value> </list> </property> </bean> </constructor-arg> </bean> </list> </property> </bean> </beans>
applicationContext.xml声明了一个bean,名restTemplate,implemented by org.springframework.web.client.RestTemplate,RestTemplate 类提供了一个setter来声明message converters,在这个属性我们提供一个包含一个简单bean的list:a MarshallingHttpMessageConverter,这是你的实体信息声明的地方
restTemplate bean声明后,ArticleClient 使用了restTemplate来调取ArticleService:
package com.informit.articleservice.client; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import com.informit.articleservice.model.Article; import com.informit.articleservice.model.Category; @Component("articleClient") public class ArticleClient { @Autowired protected RestTemplate restTemplate; private final static String articleServiceUrl = "http://localhost:8082/articleservice/"; @SuppressWarnings("unchecked") public List<Category> getCategories() { return restTemplate.getForObject(articleServiceUrl + "article", List.class); } public Article getArticle(String category, int id) { return restTemplate.getForObject(articleServiceUrl + "article/{category}/{id}", Article.class, category, id); } @SuppressWarnings("unchecked") public void delCategories() { restTemplate.delete(articleServiceUrl + "article"); } @SuppressWarnings("unchecked") public List<Category> postCategories() { Map<String, String> params = new HashMap<String, String>(); params.put("name", "jizhong"); return restTemplate.postForObject(articleServiceUrl + "addarticle/{name}", null, List.class, params); } }
在这里RestTemplate是自动加载的(auto-wired),你会注意到ArticleClient被加上了@Component annotation而且applicationContext.xml自动扫描com.informit.articleservice包或他的子包,因此当ArticleClient通过application context被loaded时,他会自动作为一个接口来实现RestTemplate实例
RestTemplate的相关使用的方法在文档中是这样写的:
delete(): deletes an object hosted by the web service
getForObject(): executes the HTTP GET command and returns the requested object
headForHeaders(): executes the HTTP HEAD command and returns the headers for the requested service
optionsForAllow(): executes the HTTP OPTIONS command and returns list of content types the the request service allows
postForLocation: executes the HTTP POST command and returns the location header value
postForObject(): executes the HTTP POST command and returns the object at the specified URL
put(): executes the HTTP PUT command and sends the specified object to the web service
execute(): provides fine grained control if one of the aforementioned methods does not suit your needs
接下来列出测试类:
package com.informit.resttemplateexample; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.informit.articleservice.client.ArticleClient; import com.informit.articleservice.model.Category; public class RestTemplateExample { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); ArticleClient articleClient = applicationContext.getBean("articleClient", ArticleClient.class); //get operate // Article article = articleClient.getArticle("fun", 1); // System.out.println("Article: " + article.getBody()); // // List<Category> categories = articleClient.getCategories(); // for (Category category : categories) { // System.out.println("Category: " + category); // } //delete operate //articleClient.delCategories(); //post operate List<Category> categories = articleClient.postCategories(); } }
好了,然后本地跑一下就可以了,当然前提是一定把服务端跑起来哦....
注:详细代码在附件,JAR包还是自己下哈,终于写完了,有点累,但是有收获。
- resttemplateexample_20100223.rar (13.2 KB)
- 下载次数: 662
发表评论
-
spring框架下配置lucene
2012-02-21 15:34 8586最近这的是投入不少精力在lucene身上,学到一点心得,留此文 ... -
Task Scheduling Simplifications in Spring 3.0
2011-05-19 14:47 2568简单说下基本配置: applicationContex ... -
安装ActiveMQ
2011-04-18 10:28 1186安装activeMQ前先升级jdk到6U24 cd /App ... -
ECLIPSE远程DEBUG
2011-04-18 10:07 1415在服务器上添加监听端 ... -
apache的commons-net实现FTP上传,下载文件
2011-04-15 11:25 2681不多说,上代码: package com.book.s ... -
tdd:测试驱动WEB组件
2011-04-05 21:37 1689相信大家有好多人会使用JUNIT进行单元测试了,但是在WEB开 ... -
利用GeneratedKeyHolder获得新建数据主键值
2010-12-24 14:37 7085有时候我们向DB新插入一条数据,都需要获取对应的主键值。在采用 ... -
dbunit解决XML数据中必须表首行有相应字段问题
2010-11-30 09:51 1690DbUnit设计理念: 熟悉单 ... -
jMock Cookbook 中文版
2010-04-29 10:06 2026他娘的,中文版啊!爽,坚决分享! 这里不多介绍JMOC ... -
linux下配置JDK,tomcat
2010-02-26 15:34 999转自:http://blog.csdn.net/strivem ... -
hsqldb
2010-02-25 00:02 1208import java.sql.Connection; im ... -
RESTful Web Services in Spring 3(上)
2010-02-23 10:19 11240通过本文,我将介绍REST的特点,基本设计原则及其简单讲解,最 ... -
spring3实现MVC的rest
2010-02-07 15:21 8487首先配置WEB.XML: <?xml versi ... -
使用 Spring 2.5 基于注解驱动的 Spring MVC
2010-02-07 13:09 848来自地址:https://www.ibm. ... -
log4j配置
2010-01-28 14:07 1118<!-- logging begin--> ... -
java中org.apache.commons.digester类用法
2010-01-28 10:52 1469xml文件内容: <?xml version=& ... -
lucene的索引建立及查找
2010-01-27 11:03 999package lucene; import org.a ... -
java实现webservice实例
2010-01-25 18:48 131645今天下午突然想研究下WEBSERVICE,从网上找了好多实例, ... -
Hibernate批量插入的方法
2010-01-22 14:46 1455Session session = sessionFact ... -
编写你的第一个Hibernate程序
2010-01-20 11:51 1161注:转载http://www.nit-pro.org/jszl ...
相关推荐
Building RESTful Web Services with Spring 5 – Second Edition: Leverage the power of Spring 5.0, Java SE 9, and Spring Boot 2.0 Find out how to implement the REST architecture to build resilient ...
Building RESTful Web Services with Spring 5(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除查看此书详细信息请在美国亚马逊官网搜索此书
Building a RESTful Web Service with Spring 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权...
Building RESTful Web Services with Spring 5(2nd)_Code 源码 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除查看此书详细信息请在美国亚马逊官网搜索此书
Building RESTful Web Services with Go:Initially, SOAP-based web services became more popular with XML. Then, since 2012,REST picked up the pace and gulped SOAP in whole. The rise of a new generation ...
NULL 博文链接:https://liyaxi.iteye.com/blog/1463595
使用Spring 3 来创建 RESTful Web Services
使用Spring+Maven 进行RESTFul风格WebServices接口开发
Stream API- Reactive programming using RxJava 2 and Reactor- Spring WebFlux- Reactive support in Spring Data MongoDB and Spring Security- Developing reactive RESTful web services using Spring WebFlux...
使用_Spring_3_来创建_RESTful_Web_Services
Spring是一个得到广泛应用的Java EE框架,它在版本3以后就增加了RESTful Web Services开发的支持。虽然,对REST的支持并不是JAX-RS的一种实现,但是它具有比标准定义更多的特性。REST支持被无缝整合到Spring的MVC...
Chapter 12 –Developing RESTful web services using Spring Web MVC Chapter 13 – More Spring Web MVC – internationalization, file upload and asynchronous request processing Chapter 14 – Securing ...
springboot-crud-restful-webservicesYouYube视频Spring Boot,MySQL,JPA,Hibernate Restful CRUD API教程,为博客文章
Spring Security Third Edition Secure your web applications, RESTful services, and microservice architectures
使用Java和Spring Boot构建RESTful Web服务 :check_mark_button:SpringBoot :check_mark_button:SpringWeb :check_mark_button: Spring Data JPA :check_mark_button: Hibernate验证 :check_mark_button:Spring...
You will learn about several Spring projects such as Spring Boot, Spring MVC, Spring Data JPA, and Spring Security and the role they play in simplifying REST application development. You will learn ...
Exam topics covered include tasks and scheduling, remoting, the Spring Web Services framework, RESTful services with Spring MVC, the Spring JMS module, JMS and JTA transactions with Spring, batch ...
Moving ahead, you'll build a RESTful web services application using Spring WebFlux framework. You'll be then taken through creating a Spring Boot-based simple blog management system, which uses ...
带有Spring Boot和Spring Cloud的Master ... 从Restful Web Services到带有Spring Boot和Spring Cloud和Ranga Karanam以及in28Minutes的微服务的绝妙旅程-Udemy课程,具有Spring Boot和Spring Cloud的微服务