之前介绍过利用JAX-WS方式发布webservice,今天简单介绍一下cxf。
第一步,创建java project
cxf相关的各种jar包内提供了丰富的功能,仅仅创建java project即可
第二步,引入相关jar包http://download.csdn.net/detail/duanml61/5068049
1.commons-logging-1.1.1.jar
2.geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar)
3.geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)
4.geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar)
5.geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar)
6.geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
7.geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)
8.geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
9.jaxb-api-2.1.jar
10.jaxb-impl-2.1.12.jar
11.jetty-6.1.21.jar
12.jetty-util-6.1.21.jar
13.neethi-2.0.4.jar
14.saaj-api-1.3.jar
15.saaj-impl-1.3.2.jar
16.wsdl4j-1.6.2.jar
17.wstx-asl-3.2.8.jar
18.XmlSchema-1.4.5.jar
19.xml-resolver-1.2.jar
20.cxf-2.2.2.jar
21.commons-logging-1.1.1.jar
22geronimo-activation_1.1_spec-1.0.2.jar (or Sun's Activation jar)
23. geronimo-annotation_1.0_spec-1.1.1.jar (JSR 250)
24.geronimo-javamail_1.4_spec-1.6.jar (or Sun's JavaMail jar)
25. geronimo-servlet_2.5_spec-1.2.jar (or Sun's Servlet jar)
26.geronimo-ws-metadata_2.0_spec-1.1.2.jar (JSR 181)
27.geronimo-jaxws_2.1_spec-1.0.jar (or Sun's jaxws-api-2.1.jar)
28.geronimo-stax-api_1.0_spec-1.0.1.jar (or other stax-api jar)
29.jaxb-api-2.1.jar
30. jaxb-impl-2.1.12.jar
31. jetty-6.1.21.jar
32. jetty-util-6.1.21.jar
33. neethi-2.0.4.jar
34.saaj-api-1.3.jar
35. saaj-impl-1.3.2.jar
36. wsdl4j-1.6.2.jar
37. wstx-asl-3.2.8.jar
38. XmlSchema-1.4.5.jar
39. xml-resolver-1.2.jar
40. cxf-2.2.2.jar
第三步,新建一个webservice接口
package com.xjtu.broadcast; /** * * * <p> * Title: 测试代码 /p> * * <p> * Description: 示例 业务类 * </p> * * <p> * Copyright: Copyright (c) 2012 * </p> * * @author dml@2013-1-11 * @version 1.0 */ public interface BroadcastService { public void sendBroadcastMsg(String message, String groupname); }
第四步,实现WebService接口
package com.xjtu.broadcast; import org.jivesoftware.smack.Connection; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.Message; /** * * * <p> * Title: 测试代码 /p> * * <p> * Description: 示例 业务类 * </p> * * <p> * Copyright: Copyright (c) 2012 * </p> * * @author dml@2013-1-11 * @version 1.0 */ public class BroadcastServiceImpl implements BroadcastService { /** * 获取连接 * * @return connection */ public static Connection getConnection() { String ip = "127.0.0.1"; Integer dk = 5222; ConnectionConfiguration config = new ConnectionConfiguration(ip, dk); Connection connection = new XMPPConnection(config); return connection; } /** * 发送广播消息 * * @param message * [消息内容] * @param groupname * [组名] */ public void sendBroadcastMsg(String message, String groupname) { try { Connection con = BroadcastServiceImpl.getConnection(); con.connect(); con.loginAnonymously();// 匿名登录。 System.out.println("Authenticated = " + con.isAuthenticated() + " " + con.getUser()); Message m = new Message(); m.setBody(message);// 设置消息。 m.setTo(groupname + "@broadcast.jointsky");// [groupname]@[serviceName].[serverName] con.sendPacket(m); } catch (XMPPException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
第五步,创建服务端
package com.xjtu.broadcast; import org.apache.cxf.frontend.ServerFactoryBean; /** * * * <p> * Title: 测试代码 /p> * * <p> * Description: 示例 业务类 * </p> * * <p> * Copyright: Copyright (c) 2012 * </p> * * @author dml@2013-1-11 * @version 1.0 */ public class Server { public static void main(String[] args) { BroadcastServiceImpl bsi = new BroadcastServiceImpl(); ServerFactoryBean factoryBean = new ServerFactoryBean(); factoryBean.setAddress("http://localhost:8080/broadcast"); factoryBean.setServiceClass(BroadcastService.class); factoryBean.setServiceBean(bsi); factoryBean.create(); } }
第六步,运行服务端程序,打开浏览器,在地址栏中输入http://localhost:8080/broadcast?wsdl
第七步,创建客户端,调用相关服务
dml@2013.2.17