在javaeye里参考大牛们的帖子去配置spring2.5+hibernate3.2+CXF2.1,最终解决了路径问题后,终于成功了!
CXF是Apache的一个重点项目,终于放出来了,因为它跟spring的结合很方便,于是就用一下,还是要自己动手一步步弄一下,才知道其配置上的一些小细节(如果用插件的话,就感觉不到它跟spring的关系和不知道它的很多细节,所以推荐不要用插件的好)。
在这里顺带提一下,spring2.5结合junit4.4可以很容易地运用annotation来进行testcase的编写,不过要注意的是eclipse3.3或者eclipose3.4,里头自带的junit4是junit4.3版本的,缺少需要的方法,所以要去下载最新的junit4.4版,然后替换掉eclipse插件里的junit.jar包。
- 准备依赖包,依赖包不用想那么多,我在这里是把握的项目包弄个截图,所以是很多的,重点是spring,hibernate,CXF*这些包。
- 在web.xml文件中添加以下servlet配置:
<!--
CXF 配置
-->
<
servlet
>
<
servlet-name
>
CXFServlet
</
servlet-name
>
<
servlet-class
>
org.apache.cxf.transport.servlet.CXFServlet
</
servlet-class
>
<
load-on-startup
>
1
</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
CXFServlet
</
servlet-name
>
<
url-pattern
>
/services/*
</
url-pattern
>
</
servlet-mapping
>
- 添加applicationContext-cxf.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:jaxws
="http://cxf.apache.org/jaxws"
xsi:schemaLocation
="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-autowire
="byName"
default-lazy-init
="true"
>
<
description
>
基于Apache CXF的Web Service配置文件
</
description
>
<
import
resource
="classpath:META-INF/cxf/cxf.xml"
/>
<
import
resource
="classpath:META-INF/cxf/cxf-servlet.xml"
/>
<
import
resource
="classpath:META-INF/cxf/cxf-extension-soap.xml"
/>
<
import
resource
="classpath:META-INF/cxf/cxf-extension-javascript-client.xml"
/>
<
bean
id
="helloWorldImpl"
class
="com.lbg.ws.test.impl.HelloWorld"
/>
<
jaxws:endpoint
id
="helloWorld"
implementor
="#helloWorldImpl"
address
="/HelloWorld"
/>
</
beans
>
- 建立webservice的接口与实现:
package
com.lbg.ws.test;
import
javax.jws.WebMethod;
import
javax.jws.WebService;
@WebService(name
=
"
HelloWorld
"
)
public
interface
IHelloWorld
{
@WebMethod(operationName
=
"
sayHello
"
)
public
String sayHello();
}
package
com.lbg.ws.test.impl;
import
javax.jws.WebService;
import
com.lbg.ws.test.IHelloWorld;
@WebService(endpointInterface
=
"
com.lbg.ws.test.IHelloWorld
"
)
public
class
HelloWorld
implements
IHelloWorld
{

public
String sayHello()
{
return
"
HelloWorld!
"
;
}
}
- 把项目部署到tomcat或其它j2ee容器上启动,成功信息如下:
2008
-
6
-
30
21
:
16
:
57
org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://impl.test.ws.lbg.com/}HelloWorldService from class com.lbg.ws.test.IHelloWorld
2008
-
6
-
30
21
:
16
:
57
org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be /HelloWorld
好了,那么样才能够看到wsdl文档呢?关键就在web.xml配置servlet那里,
< servlet-mapping >
<
servlet-name
>
CXFServlet
</
servlet-name
>
<
url-pattern
>
/services/*
</
url-pattern
>
</ servlet-mapping >
这个mapping里的<url-pattern>就是你的所有webservice的访问路径了,而在applicationContext-cxf.xml中定义的服务RUL是"/HelloWorld",你的应用服务是这样:http://localhost:8080/testProject/,那么上面的webservice访问路径就是http://localhost:8080/testProject/services/HelloWorld?wsdl。
1万+

被折叠的 条评论
为什么被折叠?



