您的位置:首页 > 运维架构 > Apache

使用Apache CXF和Spring集成创建Web Service(zz)

2013-12-20 11:42 295 查看

使用ApacheCXF和Spring集成创建WebService

您的评价:还行
收藏该经验
1.创建HelloWorld接口类

查看源码

打印?

1
package
com.googlecode.garbagecan.cxfstudy.helloworld;
2
import
javax.jws.WebParam;
3
import
javax.jws.WebResult;
4
import
javax.jws.WebService;
5
@WebService
6
public
interface
HelloWorld{
7
public
@WebResult
(name=
"String"
)StringsayHi(
@WebParam
(name=
"text"
)Stringtext);
8
}
2.创建HelloWorld实现类

1
package
com.googlecode.garbagecan.cxfstudy.helloworld;
2
public
class
HelloWorldImpl
implements
HelloWorld{
3
public
StringsayHi(Stringname){
4
Stringmsg=
"Hello"
+name+
"!"
;
5
System.out.println(
"Server:"
+msg);
6
return
msg;
7
}
8
}
3.修改web.xml文件

01
<!DOCTYPEweb-appPUBLIC
02
"-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN"
03
"http://java.sun.com/dtd/web-app_2_3.dtd">
04
<
web-app
>
05
<
display-name
>cxfstudy</
display-name
>
06
<
servlet
>
07
<
servlet-name
>cxf</
servlet-name
>
08
<
servlet-class
>org.apache.cxf.transport.servlet.CXFServlet</
servlet-class
>
09
<
load-on-startup
>1</
load-on-startup
>
10
</
servlet
>
11
<
servlet-mapping
>
12
<
servlet-name
>cxf</
servlet-name
>
13
<
url-pattern
>/ws/*</
url-pattern
>
14
</
servlet-mapping
>
15
<
listener
>
16
<
listener-class
>org.springframework.web.context.ContextLoaderListener</
listener-class
>
17
</
listener
>
18
19
<
context-param
>
20
<
param-name
>contextConfigLocation</
param-name
>
21
<
param-value
>classpath*:**/spring.xml</
param-value
>
22
</
context-param
>
23
24
</
web-app
>
4.创建spring配置文件并放在classpath路径下

01
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
03
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws
=
"http://cxf.apache.org/jaxws"
04
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
'target='_blank'>http://cxf.apache.org/schemas/jaxws.xsd">[/code]
05
http://cxf.apache.org/jaxws
06
<
import
resource
=
"classpath:META-INF/cxf/cxf.xml"
/>
07
<
import
resource
=
"classpath:META-INF/cxf/cxf-extension-soap.xml"
/>
08
<
import
resource
=
"classpath:META-INF/cxf/cxf-servlet.xml"
/>
09
<
jaxws:endpoint
id
=
"helloworld"
implementor
=
"com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorldImpl"
address
=
"/HelloWorld"
/>
10
11
<!--Forclienttest-->
12
<
jaxws:client
id
=
"helloworldClient"
address
=
"http://localhost:9000/ws/HelloWorld"
serviceClass
=
"com.googlecode.garbagecan.cxfstudy.helloworld.HelloWorld"
/>
13
</
beans
>
5.创建测试类

01
package
com.googlecode.garbagecan.cxfstudy.helloworld;
02
import
org.springframework.context.ApplicationContext;
03
import
org.springframework.context.support.ClassPathXmlApplicationContext;
04
public
class
SpringClient{
05
public
static
void
main(String[]args){
06
ApplicationContextcontext=
new
ClassPathXmlApplicationContext(
"spring.xml"
);
07
HelloWorldhelloworld=(HelloWorld)context.getBean(
"helloworldClient"
);
08
System.out.println(helloworld.sayHi(
"kongxx"
));
09
}
10
}
6.测试

6.1.首先启动tomcat或者使用maven的jetty,并访问http://localhost:9000/ws/HelloWorld?wsdl来验证webservice已经启动并且生效;
6.2.然后运行测试类来验证webservice。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: