- 浏览: 532387 次
- 性别:
- 来自: 北京
-
文章分类
- 全部博客 (422)
- 重要 (12)
- BUG解决备忘录 (32)
- 环境搭建 (17)
- 开源组件 (4)
- 数据库 (16)
- 设计模式 (4)
- 测试 (3)
- javascript (5)
- Android (14)
- jdk相关 (9)
- struts2 (10)
- freemark (3)
- 自定义扩展及工具类 (5)
- jdk5新特性及java基础 (13)
- ssh及其他框架 (15)
- linux (32)
- tcp-ip http协议 (8)
- 服务器集群与负载均衡 (34)
- 项目管理相关 (11)
- 实用小技术 (10)
- 架构相关 (14)
- firefox组件 (11)
- spider (6)
- 产品设计 (11)
- PHP (1)
- ws (4)
- lucene (10)
- 其他 (2)
- BI (1)
- NoSQL (3)
- gzip (1)
- ext (4)
- db (6)
- socket (1)
- 源码阅读 (2)
- NIO (2)
- 图片处理 (1)
- java 环境 (2)
- 项目管理 (4)
- 从程序员到项目经理(一):没有捷径 (1)
- bug (1)
- JAVA BASE (8)
- 技术原理 (0)
- 新框架新技术 (1)
- 量化与python (1)
- 系统编程 (0)
- C语言 (0)
- 汇编 (0)
- 算法 (0)
最新评论
-
hyspace:
别逗了,最后一个算法根本不是最优的,sort(function ...
数组去重——一道前端校招试题 -
washingtin:
楼主能把策略和路由的类代码贴出来吗
Spring + iBatis 的多库横向切分简易解决思路 -
sdyjmc:
初略看了一下,没有闹明白啊,均衡负载使用Nginx,sessi ...
J2EE集群原理 I -
shandeai520:
谢谢大神!请教大神一个问题:假如我有三台服务器,连接池的上限是 ...
集群和数据库负载均衡的研究 -
hekuilove:
给lz推荐一下apache commonsStringUtil ...
request 获取 ip
前面介绍过Spring的MVC结合不同的view显示不同的数据,如:结合json的 view显示json、结合xml的view显示xml文档。那么这些数据除了在WebBrowser中用JavaScript来调用以外,还可以用远程 服务器的Java程序、C#程序来调用。也就是说现在的程序不仅在BS中能调用,在CS中同样也能调用,不过你需要借助RestTemplate这个类来 完成。RestTemplate有点类似于一个WebService客户端请求的模版,可以调用http请求的WebService,并将结果转换成相应 的对象类型。至少你可以这样理解!
上一次博文介绍SpringMVC结合不同的View,显示不同的数据。http://www.cnblogs.com/hoojo/archive/2011/04/29/2032571.html
Email:hoojo_@126.com
Blog:http://blog.csdn.net/IBM_hoojo
一、准备工作
1、 下载jar包
spring各版本jar下载地址:http://ebr.springsource.com/repository/app/library/detail?name=org.springframework.spring
相关的依赖包也可以在这里找到:http://ebr.springsource.com/repository/app/library
2、 需要jar包如下
3、 当前工程的web.xml配置
<? xml version ="1.0" encoding ="UTF-8" ?>
< web-app version ="2.4"
xmlns ="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 配置Spring核心控制器 -->
< servlet >
< servlet-name > dispatcher</ servlet-name >
< servlet-class > org.springframework.web.servlet.DispatcherServlet</ servlet-class >
< init-param >
< param-name > contextConfigLocation</ param-name >
< param-value > /WEB-INF/dispatcher.xml</ param-value >
</ init-param >
< load-on-startup > 1</ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name > dispatcher</ servlet-name >
< url-pattern > *.do</ url-pattern >
</ servlet-mapping >
< welcome-file-list >
< welcome-file > index.jsp</ welcome-file >
</ welcome-file-list >
</ web-app >
4、 WEB-INF中的dispatcher.xml配置
<? xml version ="1.0" encoding ="UTF-8" ?>
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:mvc ="http://www.springframework.org/schema/mvc"
xmlns:context ="http://www.springframework.org/schema/context"
xmlns:util ="http://www.springframework.org/schema/util"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
< context:component-scan base-package ="com.hoo.*" >
<!-- 忽略这个类 -->
< context:exclude-filter type ="assignable" expression ="com.hoo.client.RESTClient" />
</ context:component-scan >
<!-- annotation的方法映射适配器 -->
< bean id ="handlerAdapter" class ="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- xml视图,XStreamMarshaller,可以转换任何形式的java对象 -->
< bean name ="xStreamMarshallingView" class ="org.springframework.web.servlet.view.xml.MarshallingView" >
< property name ="marshaller" >
< bean class ="org.springframework.oxm.xstream.XStreamMarshaller" >
<!-- 为了初始化XStreamMarshaller,这个类会把我们接口中得到结果以XML文档形式展现出来 -->
< property name ="autodetectAnnotations" value ="true" />
</ bean >
</ property >
</ bean >
<!-- 视图解析器,根据视图的名称new ModelAndView(name),在配置文件查找对应的bean配置 -->
< bean class ="org.springframework.web.servlet.view.BeanNameViewResolver" >
< property name ="order" value ="3" />
</ bean >
<!-- annotation默认的方法映射适配器 -->
< bean id ="handlerMapping" class ="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
< property name ="order" value ="1" />
</ bean >
</ beans >
5、 启动后,可以看到index.jsp 没有出现异常或错误。那么当前SpringMVC的配置就成功了。
二、REST控制器实现
REST控制器主要完成CRUD操作,也就是对于http中的post、get、put、delete。
还有其他的操作,如head、options、trace。
具体代码:
package
com.hoo.controller;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.PathVariable;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.servlet.ModelAndView;
/**
* <b>function:</b>SpringMVC REST示例
* @author hoojo
* @createDate 2011-6-9 上午11:34:08
* @file RESTController.java
* @package com.hoo.controller
* @project SpringRestWS
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
@RequestMapping("/restful"
)
@Controller
public class RESTController {
@RequestMapping(value = "/show"
, method = RequestMethod.GET)
public
ModelAndView show() {
System.out.println("show"
);
ModelAndView model = new ModelAndView("xStreamMarshallingView" );
model.addObject("show method"
);
return
model;
}
@RequestMapping(value = "/get/{id}"
, method = RequestMethod.GET)
public
ModelAndView getUserById(@PathVariable String id) {
System.out.println("getUserById-"
+ id);
ModelAndView model = new ModelAndView("xStreamMarshallingView" );
model.addObject("getUserById method -"
+ id);
return
model;
}
@RequestMapping(value = "/add"
, method = RequestMethod.POST)
public
ModelAndView addUser(String user) {
System.out.println("addUser-"
+ user);
ModelAndView model = new ModelAndView("xStreamMarshallingView" );
model.addObject("addUser method -"
+ user);
return
model;
}
@RequestMapping(value = "/edit"
, method = RequestMethod.PUT)
public
ModelAndView editUser(String user) {
System.out.println("editUser-"
+ user);
ModelAndView model = new ModelAndView("xStreamMarshallingView" );
model.addObject("editUser method -"
+ user);
return
model;
}
@RequestMapping(value = "/remove/{id}"
, method = RequestMethod.DELETE)
public
ModelAndView removeUser(@PathVariable String id) {
System.out.println("removeUser-"
+ id);
ModelAndView model = new ModelAndView("xStreamMarshallingView" );
model.addObject("removeUser method -"
+ id);
return
model;
}
}
上面的方法对应的http操作:
/show -> get 查询
/get/id -> get 查询
/add -> post 添加
/edit -> put 修改
/remove/id -> delete 删除
在这个方法中,就可以看到RESTful风格的url资源标识
@RequestMapping(value = "/get/{id}"
, method = RequestMethod.GET)
public
ModelAndView getUserById(@PathVariable String id) {
System.out.println("getUserById-"
+ id);
ModelAndView model = new ModelAndView("xStreamMarshallingView" );
model.addObject("getUserById method -"
+ id);
return
model;
}
value=”/get/{id}”就是url中包含get,并且带有id参数的get请求,就会执行这个方法。这个url在请求的时候,会通过 Annotation的@PathVariable来将url中的id值设置到getUserById的参数中去。 ModelAndView返回的视图是xStreamMarshallingView是一个xml视图,执行当前请求后,会显示一篇xml文档。文档的内 容是添加到model中的值。
三、利用RestTemplate调用REST资源
代码如下:
package
com.hoo.client;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Component;
import
org.springframework.web.client.RestTemplate;
/**
* <b>function:</b>RestTemplate调用REST资源
* @author hoojo
* @createDate 2011-6-9 上午11:56:16
* @file RESTClient.java
* @package com.hoo.client
* @project SpringRestWS
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
@Component
public class RESTClient {
@Autowired
private
RestTemplate template;
private final static String url = "http://localhost:8080/SpringRestWS/restful/" ;
public
String show() {
return template.getForObject(url + "show.do" , String.class , new String[]{});
}
public
String getUserById(String id) {
return template.getForObject(url + "get/{id}.do" , String.class , id);
}
public
String addUser(String user) {
return template.postForObject(url + "add.do?user={user}" , null, String.class , user);
}
public
String editUser(String user) {
template.put(url + "edit.do?user={user}"
, null, user);
return
user;
}
public
String removeUser(String id) {
template.delete(url + "/remove/{id}.do"
, id);
return
id;
}
}
RestTemplate的getForObject完成get请求、postForObject完成post请求、put对应的完成put请求、 delete完成delete请求;还有execute可以执行任何请求的方法,需要你设置RequestMethod来指定当前请求类型。
RestTemplate.getForObject(String url, Class<String> responseType, String... urlVariables)
参数url是http请求的地址,参数Class是请求响应返回后的数据的类型,最后一个参数是请求中需要设置的参数。
template.getForObject(url + "get/{id}.do", String.class, id);
如上面的参数是{id},返回的是一个string类型,设置的参数是id。最后执行该方法会返回一个String类型的结果。
下面建立一个测试类,完成对RESTClient的测试。代码如下:
package
com.hoo.client;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.test.context.ContextConfiguration;
import
org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;
/**
* <b>function:</b>RESTClient TEST
* @author hoojo
* @createDate 2011-6-9 下午03:50:21
* @file RESTClientTest.java
* @package com.hoo.client
* @project SpringRestWS
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
@ContextConfiguration("classpath:applicationContext-*.xml"
)
public class RESTClientTest extends AbstractJUnit38SpringContextTests {
@Autowired
private
RESTClient client;
public void testShow() {
System.out.println(client.show());
}
public void testGetUserById() {
System.out.println(client.getUserById("abc"
));
}
public void testAddUser() {
System.out.println(client.addUser("jack"
));
}
public void testEditUser() {
System.out.println(client.editUser("tom"
));
}
public void testRemoveUser() {
System.out.println(client.removeUser("aabb"
));
}
}
我们需要在src目录下添加applicationContext-beans.xml完成对restTemplate的配置。restTemplate需要配置MessageConvert将返回的xml文档进行转换,解析成JavaObject。
<? xml version ="1.0" encoding ="UTF-8" ?>
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:context ="http://www.springframework.org/schema/context"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
< context:component-scan base-package ="com.hoo.*" />
< bean id ="restTemplate" class ="org.springframework.web.client.RestTemplate" >
< property name ="messageConverters" >
< list >
< bean class ="org.springframework.http.converter.xml.MarshallingHttpMessageConverter" >
< property name ="marshaller" ref ="xStreamMarshaller" />
< property name ="unmarshaller" ref ="xStreamMarshaller" />
</ bean >
</ list >
</ property >
</ bean >
< bean id ="xStreamMarshaller" class ="org.springframework.oxm.xstream.XStreamMarshaller" >
< property name ="annotatedClasses" >
< array >
</ array >
</ property >
</ bean >
</ beans >
上面配置了xStreamMarshaller是和RESTController中的ModelAndView的view对应的。因为那边是用 xStreamMarshaller进行编组的,所以RestTemplate这边也需要用它来解组。RestTemplate还指出其他的 MarshallingHttpMessageConverter;
发表评论
-
求鱼不如求渔 Welcome to MICKY's HOME Spring中加载ApplicationContext.xml文件的方式【转
2013-03-08 10:43 8531.利用ClassPathXmlApplication ... -
小试Bean Validation
2012-03-18 21:56 885数据校验是任何一个应用程序都会用到的功能,无论是显示层还是持久 ... -
Hibernate-Validation的使用
2012-03-18 11:56 1475以前弄过hibernate-validation的,可今天 ... -
springMVC3 基于注解的输入验证
2012-03-18 00:51 1634在pom.xml中加入,他需要validation-api ... -
pring3.0支持restful实例
2012-03-16 11:59 1115最近在研究spring3.0以及传说中的restful,还 ... -
设计 REST 风格的 MVC 框架
2012-03-16 11:38 971简介: 传统的 JavaEE MVC 框架如 Struts ... -
Spring mvc 构造RESTful URL 详细讲解(spring 3.0 应用)
2012-03-16 11:34 2361详细讲解spring rest使用,简单例子如下: / ... -
Spring MVC REST 例子
2012-03-16 03:17 1255package com.benx; ... -
ibatis存储过程调用(转载)
2011-08-14 17:11 11141 ibatis 调用oracle函数示例 2 ... -
通通透透理解ThreadLocal
2011-02-23 18:06 749概述 我们知道Spring通过各种DAO模板类降低了开发者使 ... -
乐观锁与悲观锁
2011-02-23 17:49 883文章转自网上好像是玉米田的,忘记了锁( locking ) 业 ... -
spring 依赖注入到直接new 对象
2010-09-16 15:50 1030当为遗留系统加入spring时,经典问题就是遗留系统需要引用s ... -
Spring注解入门
2010-06-03 17:11 8621. 使用Spring注解来注入属性 1.1. 使用注解 ... -
Spring2.0用注解实现事务管理
2010-06-03 15:50 918Spring2.0 框架的事务处理有两大类: 1 ...
相关推荐
Spring REST Docs。 官网 Spring REST Docs API。 Spring REST Docs 开发文档。
spring rest 增删改查(put\delete\post\get)简单示例详细讲解,亲测可用。spring rest 增删改查(put\delete\post\get)简单示例详细讲解,亲测可用。spring rest 增删改查(put\delete\post\get)简单示例详细...
spring rest doc 例子,根据里面的例子,配合你的项目我相信能很快就用的上哦。尝试着用用吧。 spring rest doc 例子,根据里面的例子,配合你的项目我相信能很快就用的上哦。尝试着用用吧。
Spring REST Docs 扩展项目是一个为简化RESTful API文档编写而设计的工具集。它提供了一种快速编写API文档的方法,特别是在Java环境中使用Spring框架开发RESTful服务时,能极大地提高开发效率和文档的质量。该项目...
标题中的"jersey+spring rest webservice web maven"是一个基于Java开发的RESTful Web服务架构,它结合了几个关键的技术组件。让我们深入探讨每个部分: 1. **Jersey**: Jersey是Java RESTful Web服务(JAX-RS)...
`RestTemplate`是Spring提供的一种简单易用的HTTP客户端工具,用于发起HTTP请求并与REST服务进行交互。本文将深入探讨`RestTemplate`的使用方法,以及如何在实际项目中有效地利用它。 首先,`RestTemplate`是Spring...
Spring Rest2ts生成器 spring-rest2ts-generator基于Spring MVC REST控制器和HTTP请求和响应的数据模型生成TypeScript代码。 spring-rest2ts-generator从Spring MVC开始,但是我们注意到它也很容易支持JAX-RS注释,...
Restdocs规范生成支持用法首先转到项目,然后按照说明设置Spring REST Docs扩展。 该扩展名将为您记录的每个resource.json生成resource.json文件。 您还将注意到,该项目提供了gradle插件,可用于读取所有resource....
**Spring 3.0 REST 实例详解** 在现代Web开发中,REST(Representational State Transfer,表述性状态转移)已经成为构建API的首选架构风格。Spring框架,作为一个强大的Java企业级应用开发框架,从3.0版本开始就...
“Spring REST Book”的源代码,Apress Publishing 概述 此存储库包含 [Spring REST] ( ) 书中引用的源代码。 组织 源代码由各个章节组织。 在 ChapterX 文件夹中可以找到与每个章节对应的代码,其中 X 代表章节编号...
本文主要介绍了使用Spring框架构建REST API的全过程,内容包括了从基础的Spring Web应用搭建,到使用Java配置,再到通过Spring Security进行安全认证和授权,以及REST API的其他重要特性,例如:使用HTTP消息转换器...
该示例项目将使用Spring REST后端和AngularJS前端。 问题描述-多个用户登录系统。 它们具有通知窗口(消息控制台)。 基于事件,将根据业务逻辑从后端创建JMS消息并将其发布到Queue或Topic。 有些事件必须传递到用户...
在"spring mvc rest基础学习demo"中,我们将深入探讨如何使用Spring MVC构建RESTful API。 REST(Representational State Transfer,表现层状态转移)是一种网络应用程序的设计风格和开发方式,基于HTTP协议,以...
用于创建Spring Rest Web服务的样板项目(Java注释)。 该项目包含开发基于Spring的Rest Web服务的所有基本要求。 这项工作正在进行中,我将继续使用其他功能(例如缓存,安全性,身份验证/授权等)来更新项目。
在这个小例子中,我们将探讨如何使用Spring MVC来实现REST接口。 首先,让我们理解Spring MVC的基本架构。Spring MVC通过DispatcherServlet作为前端控制器,接收HTTP请求,然后根据请求映射找到相应的Controller...
Spring Auto REST文件 扩展的目的是帮助您减少编写工作-代码和文档。 您仍然可以获得与Spring REST Docs本身相同的文档。 主要好处是减少了编写并使文档更接近代码,从而提高了文档的可维护性。 在Spring REST ...
本教程将通过一个简单的例子,讲解如何利用Spring MVC创建REST服务。 首先,我们需要在项目中引入Spring MVC的相关依赖。通常,这涉及到在Maven或Gradle的配置文件中添加Spring Web和Spring MVC的依赖。例如,如果...