前面介紹過Spring的MVC結(jié)合不同的view顯示不同的數(shù)據(jù),如:結(jié)合json的view顯示json、結(jié)合xml的view顯示xml文檔。那么這些數(shù)據(jù)除了在WebBrowser中用JavaScript來調(diào)用以外,還可以用遠(yuǎn)程服務(wù)器的Java程序、C#程序來調(diào)用。也就是說現(xiàn)在的程序不僅在BS中能調(diào)用,在CS中同樣也能調(diào)用,不過你需要借助RestTemplate這個(gè)類來完成。RestTemplate有點(diǎn)類似于一個(gè)WebService客戶端請(qǐng)求的模版,可以調(diào)用http請(qǐng)求的WebService,并將結(jié)果轉(zhuǎn)換成相應(yīng)的對(duì)象類型。至少你可以這樣理解!
?
上一次博文介紹SpringMVC結(jié)合不同的View,顯示不同的數(shù)據(jù)。 http://www.cnblogs.com/hoojo/archive/2011/04/29/2032571.html
?
Email:hoojo_@126.com
Blog: http://blog.csdn.net/IBM_hoojo
?
一、準(zhǔn)備工作
1、 下載jar包
spring各版本jar下載地址: http://ebr.springsource.com/repository/app/library/detail?name=org.springframework.spring
相關(guān)的依賴包也可以在這里找到: http://ebr.springsource.com/repository/app/library
?
2、 需要jar包如下
?
3、 當(dāng)前工程的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.*"
>
<!-- 忽略這個(gè)類 -->
<
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,可以轉(zhuǎn)換任何形式的java對(duì)象 -->
<
bean
name
="xStreamMarshallingView"
class
="org.springframework.web.servlet.view.xml.MarshallingView"
>
<
property
name
="marshaller"
>
<
bean
class
="org.springframework.oxm.xstream.XStreamMarshaller"
>
<!-- 為了初始化XStreamMarshaller,這個(gè)類會(huì)把我們接口中得到結(jié)果以XML文檔形式展現(xiàn)出來 -->
<
property
name
="autodetectAnnotations"
value
="true"
/>
</
bean
>
</
property
>
</
bean
>
<!-- 視圖解析器,根據(jù)視圖的名稱new ModelAndView(name),在配置文件查找對(duì)應(yīng)的bean配置 -->
<
bean
class
="org.springframework.web.servlet.view.BeanNameViewResolver"
>
<
property
name
="order"
value
="3"
/>
</
bean
>
?
<!-- annotation默認(rèn)的方法映射適配器 -->
<
bean
id
="handlerMapping"
class
="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
>
<
property
name
="order"
value
="1"
/>
</
bean
>
</
beans
>
5、 啟動(dòng)后,可以看到index.jsp 沒有出現(xiàn)異常或錯(cuò)誤。那么當(dāng)前SpringMVC的配置就成功了。
?
二、REST控制器實(shí)現(xiàn)
REST控制器主要完成CRUD操作,也就是對(duì)于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;
}
}
上面的方法對(duì)應(yīng)的http操作:
/show -> get 查詢
/get/id -> get 查詢
/add -> post 添加
/edit -> put 修改
/remove/id -> delete 刪除
在這個(gè)方法中,就可以看到RESTful風(fēng)格的url資源標(biāo)識(shí)
@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,并且?guī)в衖d參數(shù)的get請(qǐng)求,就會(huì)執(zhí)行這個(gè)方法。這個(gè)url在請(qǐng)求的時(shí)候,會(huì)通過Annotation的@PathVariable來將url中的id值設(shè)置到getUserById的參數(shù)中去。 ModelAndView返回的視圖是xStreamMarshallingView是一個(gè)xml視圖,執(zhí)行當(dāng)前請(qǐng)求后,會(huì)顯示一篇xml文檔。文檔的內(nèi)容是添加到model中的值。
?
三、利用RestTemplate調(diào)用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調(diào)用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請(qǐng)求、postForObject完成post請(qǐng)求、put對(duì)應(yīng)的完成put請(qǐng)求、delete完成delete請(qǐng)求;還有execute可以執(zhí)行任何請(qǐng)求的方法,需要你設(shè)置RequestMethod來指定當(dāng)前請(qǐng)求類型。
RestTemplate.getForObject(String url, Class<String> responseType, String... urlVariables)
參數(shù)url是http請(qǐng)求的地址,參數(shù)Class是請(qǐng)求響應(yīng)返回后的數(shù)據(jù)的類型,最后一個(gè)參數(shù)是請(qǐng)求中需要設(shè)置的參數(shù)。
template.getForObject(url + "get/{id}.do", String.class, id);
如上面的參數(shù)是{id},返回的是一個(gè)string類型,設(shè)置的參數(shù)是id。最后執(zhí)行該方法會(huì)返回一個(gè)String類型的結(jié)果。
?
下面建立一個(gè)測試類,完成對(duì)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完成對(duì)restTemplate的配置。restTemplate需要配置MessageConvert將返回的xml文檔進(jìn)行轉(zhuǎn)換,解析成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對(duì)應(yīng)的。因?yàn)槟沁吺怯脁StreamMarshaller進(jìn)行編組的,所以RestTemplate這邊也需要用它來解組。RestTemplate還指出其他的MarshallingHttpMessageConverter;
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

