开发步骤:
1,apache.org下载CXF项目
2,解压cxf后,导入lib下的部分包:cxf、spring相关的包
3,创建接口与接口实现类
<!--接口-->
package com.gufengxiachen.cxf.service;
import
javax.jws.WebParam;
import
javax.jws.WebService;
@WebService
public
interface
MessageService {
String getMessage(
@WebParam
(name=
"text"
) String text);
}
<!--实现类-->
package
com.gufengxiachen.cxf.service.impl;
import
javax.jws.WebService;
import
com.gufengxiachen.cxf.service.MessageService;
@WebService
(endpointInterface =
"com.gufengxiachen.cxf.service.MessageService"
,
serviceName =
"MessageService"
)
public
class
MessageServiceImpl
implements
MessageService{
public
String getMessage(String text) {
//
TODO
Auto-generated method stub
return
"Welcome"
+text;
}
}
4,编写web.xml与beans.xml文件如下:
<!--web.xml-->
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
version
=
"2.5"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
/WEB-INF/beans.xml
</
param-value
>
</
context-param
>
<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>
<
servlet
>
<
servlet-name
>
CXFServlet
</
servlet-name
>
<
display-name
>
CXF Servlet
</
display-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
>
</
web-app
>
<!--beans.xml-->
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!-- START SNIPPET: beans -->
<
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://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
>
<
import
resource
=
"classpath:META-INF/cxf/cxf.xml"
/>
<
import
resource
=
"classpath:META-INF/cxf/cxf-extension-soap.xml"
/>
<
import
resource
=
"classpath:META-INF/cxf/cxf-servlet.xml"
/>
<
jaxws:endpoint
id
=
"messageService"
implementor
=
"com.gufengxiachen.cxf.service.impl.MessageServiceImpl"
address
=
"/MessageService"
/>
</
beans
>
部署在tomcat下,运行在浏览器输入:
客户端调用代码:
package
com.gufengxiachen.cxf.client;
import
org.apache.cxf.interceptor.LoggingInInterceptor;
import
org.apache.cxf.interceptor.LoggingOutInterceptor;
import
org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import
com.gufengxiachen.cxf.service.MessageService;
public
class
MessageServiceClient {
public
static
void
main(String[] args) {
JaxWsProxyFactoryBean factory =
new
JaxWsProxyFactoryBean();
factory.getInInterceptors().add(
new
LoggingInInterceptor());
factory.getOutInterceptors().add(
new
LoggingOutInterceptor());
factory.setServiceClass(MessageService.
class
);
factory.setAddress(
"http://localhost:8080/cxf/services/MessageService"
);
MessageService client = (MessageService) factory.create();
String reply = client.getMessage(
"张三"
);
System.
out
.println(
"Server said: "
+ reply);
}
}
附录:使用wsdl2java工具生成客户端代码:
cmd命令在cxf/bin目录下,运行:
wsdl2java -p 包名 -d 目录 wsdl url
例如:
wsdl2java -p com.gufengxiachen.cxf.service -d D:/cxf/client http://127.0.0.1/cxf/services/MessageService?wsdl