发布一个cxf方式的接口流程:
1.导入需要的cxf包:
cxf-2.2.9.jar
cxf-manifest.jar
cxf-xjc-boolean-2.2.9.jar
cxf-xjc-bug671-2.2.9.jar
cxf-xjc-dv-2.2.9.jar
cxf-xjc-ts-2.2.9.jar
2.在接口类上添加@WebService
如
@WebService
public interface UpdateRegisterWS {
/**
* 说明:调用此方法,更新注册信息
* @author
* 创建时间:2010-8-17 下午01:36:34
* @param id 主键
* @param version
* @param flag
* @return
*/
public void updateRegisterInfo(String id,String version,String flag);
}
接口的实现类只是一个普通的接口实现类
/**
* 说明:下发之后,更新站点注册信息的WebService接口的实现类
* @author zhl
* 创建时间:2010-8-17 上午11:49:43
*/
public class UpdateRegisterWS Impl implements UpdateRegisterWS {
UpdateRegisterWSDao updateRegisterWSDao;
public void updateRegisterInfo(String id,String version,String flag) {
String sql = "UPDATE USERREGISTER t SET t.VERSION_NO = '"+version+"',t.flag= '"+flag+"' WHERE t.REG_ID = '"+id+"'";
updateRegisterWSDao.excuteSql(sql);
}
//updateRegisterWSDao的getter setter方法
}
3
建立applicationContext-ws-service.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.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" />
<bean id="updateRegisterWSImpl" class="cn.com.sysmanage.register.service.impl.UpdateRegisterWSImpl" scope="prototype">
<property name="updateRegisterWSDao">
<ref bean="updateRegisterWSDao" />
</property>
</bean>
<jaxws:endpoint id="updateRegisterWS"
implementor="#updateRegisterWSImpl"
address="/UpdateRegisterWS"/>
</beans>
其中,id随便写,implementor为实现类,address为访问地址。
4.
在web.xml中做如下配置
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
5.启动工程访问:http://localhost:8080/MyProject/ws 就会看到接口列表。
即表示接口已发布成功
6.生成一个客户端步骤:
1 。得到wsdl的路径,例如http://localhost:8080/MyProject/ws/UpdateRegisterWS?wsdl
2 。apache-cxf-2.2.9可以用来生成客户端文件:放在E:/wsTools/目录下,找到apache-cxf-2.2.9/apache-cxf-2.2.9/bin/
3 。配置一下环境变量:%CXF_HOME%=E:/wsTools/apache-cxf-2.2.9/apache-cxf-2.2.9 并在PATH后加上;%CXF_HOME%/bin;打开cmd,输入
E:/wsTools/apache-cxf-2.2.9/apache-cxf-2.2.9/bin>wsdl2java -p cn.com.sysMgr.updateRegisterWSClient -client http://localhost:8080/MyProject/ws/UpdateRegisterWS?wsdl
执行后就会在bin目录下生成客户端类文件;也可以不配置环境变量,但需要从cmd进入到apache-cxf-2.2.9/apache-cxf-2.2.9/bin目录下再执行;
7.把生产的client文件打包,或者直接拿到调用该接口的工程中;
生成的几个文件中,有一个名是UpdateSiteRegisterWSImplService.java ,在这个文件里把接口的地址固定写死了,如有必要可以改进一下,比如把多个WS接口放到一个properties文件中,在properties文件中列出各个WS接口地址;
如:
在UpdateSiteRegisterWSImplService.java 中加入
private static String urlstr=ReadWsUrlProperties.getInstance().getWsAddr("UpdateRegisterWS");一句,为的是在一个property文件中动态地配置wsdl地址;修改url = new URL(urlstr);
ReadWsUrlProperties.java是一个类,用来从某个指定的properties文件中获得指定名称的wsdl地址,代码如下:
/**
*
*/
package cn.com.mgSys.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
*工具方法, 读取properties配置文件以获取web Service 的url地址
* webservice_url.properties
* @author
*
*/
public class ReadWsUrlProperties {
private static ReadWsUrlProperties instance = new ReadWsUrlProperties();
public String getWsAddr(String key){
if(key==null||"".equals(key)){
return "";
}
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("webservice_url.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
return p.getProperty(key);
}
public static ReadWsUrlProperties getInstance(){
return instance;
}
}
webservice_url.properties文件内容如下:
UpdateRegisterWS=http://localhost:8080/MyProject/ws/UpdateRegisterWS?wsdl
XXXXXWS=http://localhost:8080/MyProject/ws/XXXXXX?wsdl
8.生成的客户端文件中,有一个名字为XXX_Client.java的,从这个文件中找出下面几行代码,即可如下所示在客户端调用WebService接口了:
public class Test_UpdateRegisterWSClient {
public static void main(String[] args) {
final QName SERVICE_NAME = new QName("http://impl.service.register.manage.com.cn/", "UpdateRegisterWSImplService");
URL wsdlURL = UpdateRegisterWSImplService.WSDL_LOCATION;
UpdateRegisterWSImplService ss = new UpdateRegisterWSImplService(wsdlURL, SERVICE_NAME);
UpdateRegisterWS port = ss.getUpdateRegisterWSImplPort();
//port即可调用ws接口里的方法了
port.updateRegisterInfo("4028e44f2a82ee8a012a82fb5e120001", "v1.0", "1");
}
}
over。