对
WebService
不太熟习,一边看网上的资料,一边自己练习写写。呵呵
。
1.
使用
IDE: MyEclipse 6.0 MyEclipse TomCat
2.
使用
jar
包:
commons-loggin.jar , spring.jar
导入
Xfire 1.2 Core Libraries
为了进行客户端的编写导入
Xfire 1.2 HTTP Client Libraries
第一步:先把架子搭起来吧
在
web.xml
中引入
Xfire
插件,是个
servlet
<
servlet
>
<
servlet-name
>
XFireServlet
</
servlet-name
>
<
servlet-class
>
org.codehaus.xfire.spring.XfireSpringServlet
</
servlet-class
>
<
load-on-startup
>
0
</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
XFireServlet
</
servlet-name
>
<
url-pattern
>
/services/*
</
url-pattern
>
</
servlet-mapping
>
监听器和配置文件的加载:
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
/WEB-INF/applicationContext.xml /WEB-INF/xfire-servlet.xml
</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
第二步:建立接口和实现类
public
interface
Hello {
public
String say(String name);
}
public
class
HelloImpl
implements
Hello {
public
String say(String name) {
return
name +
" :
欢迎学习
WebService!!"
;
}
}
呵呵,简单吧。
第三步:文件配置
在
ApplicationContext.xml
中注入我们的接口
<
bean
id
=
"hello"
class
=
"cn.pan.com.HelloImpl"
></
bean
>
把
Xfire
的也导入进来吧
<
import
resource
=
"classpath:org/codehaus/xfire/spring/xfire.xml"
/>
在
xfire-servlet
中暴露服务接口:
<
bean
id
=
"echo"
class
=
"org.codehaus.xfire.spring.remoting.XFireExporter"
>
<
property
name
=
"serviceFactory"
>
<
ref
bean
=
"xfire.serviceFactory"
/>
</
property
>
<
property
name
=
"xfire"
>
<
ref
bean
=
"xfire"
/>
</
property
>
<
property
name
=
"serviceBean"
>
<
ref
bean
=
"hello"
/>
</
property
>
<
property
name
=
"serviceClass"
>
<
value
>
cn.pan.com.Hello
</
value
>
</
property
>
</
bean
>
注意这里的
bean xfire
和
xfire.serviceFactory
在上面导入了
xfire.xml
才能被找到。
呵呵,处理器映射:
<
bean
class
=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
>
<
property
name
=
"urlMap"
>
<
map
>
<
entry
key
=
"/hello"
>
<
ref
bean
=
"echo"
/>
</
entry
>
</
map
>
</
property
>
</
bean
>
OK
,结束。部署到
TOMCAT
中,输入你的项目地址:
看到什么了没有。有一个超链接,点击后你就可以看到
WSDL
了,
WebService
部署成功!!
第四步:客户端测试:
呵呵,差点忘了进行测试了。
建立配置文件
client.xml
<
bean
id
=
"testWebService"
class
=
"org.codehaus.xfire.spring.remoting.XFireClientFactoryBean"
>
<
property
name
=
"serviceClass"
>
<
value
>
cn.pan.com.Hello
</
value
>
</
property
>
<
property
name
=
"wsdlDocumentUrl"
>
<
value
>
</
vale
>
</
property
>
</
bean
>
在
spring
中配置客户端测试程序,你会发现程序会变得这个很简单:
FileSystemXmlApplicationContext xmlContext =
new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/client.xml");
Hello h = (Hello)xmlContext.getBean("testWebService");
System.out.println(h.say("crazy jack"));