参见文章1和文章2
(1)编写服务程序,需要cxf的包,我用的是cxf2.6.1,都是使用内置jetty发布,方法有两种:
1.使用Sun JAX-WS 2中Endpoint.publish进行发布,包含的库有cxf-api-2.6.1.jar,cxf-rt-bindings-xml-2.6.1.jar,cxf-rt-core-2.6.1.jar,cxf-rt-frontend-jaxrs-2.6.1.jar,cxf-rt-transports-http-2.6.1.jar
编写服务端代码如下:
package com.sysware.app.service.impl; import com.sysware.app.domain.RetInfo; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.jws.soap.soAPBinding; import javax.jws.soap.soAPBinding.Style; @WebService @SOAPBinding(style = Style.RPC) public class GetInfoServiceImpl { @WebMethod(operationName = "add") @WebResult(name = "result") public int add(@WebParam(name = "num1") int num1,@WebParam(name = "num2")int num2) { return num1 + num2; } @WebMethod(operationName = "getRetInfo") @WebResult(name = "result") public RetInfo getRetInfo(@WebParam(name = "name") String name,@WebParam(name = "age")int age) { RetInfo retInfo = new RetInfo(); retInfo.setAge(age); retInfo.setName(name); return retInfo; } }
发布web service接口
package com.sysware.app.service.inf; public interface IDeployWebService { void deployService(String address); }发布web service实现类
package com.sysware.app.service.impl; import com.sysware.app.service.inf.IDeployWebService; import javax.xml.ws.Endpoint; public class DeployWebServicePublishImpl implements IDeployWebService { public DeployWebServicePublishImpl() { } @Override public void deployService(String address) { GetInfoServiceImpl service = new GetInfoServiceImpl(); Endpoint.publish(address,service); } }主类
package com.sysware.app; import com.sysware.app.service.impl.GetInfoServiceImpl; import javax.xml.ws.Endpoint; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import com.sysware.app.service.impl.DeployWebServicePublishImpl; import com.sysware.app.service.inf.IDeployWebService; public class App { /** * <b>function:</b>发布WebService * * @author hoojo */ public static void deployService(String ip,int port) { GetInfoServiceImpl service = new GetInfoServiceImpl(); if (ip.length() < 1) ip = "127.0.0.1"; if (port < 1) port = 8080; String address = "http://" + ip + ":" + String.valueOf(port) + "/getInfoService"; IDeployWebService idlp = new DeployWebServicePunishImpl(); idlp.deployService(address); } public static void main(String[] args) throws InterruptedException { String ip = ""; int port = 8080; if (args.length > 0) { ip = args[0]; if (args.length > 1) port = Integer.parseInt(args[1]); } System.out.println("Server start ……"); //发布WebService deployService(ip,port); System.out.println("server ready ……"); BufferedReader br = new BufferedReader(new InputStreamReader(system.in)); while (true) { Thread.sleep(500); try { String a = br.readLine(); if ("q".equals(a)) break; else System.out.println("invalid input"); } catch (IOException e) { e.printstacktrace(); //To change body of catch statement use File | Settings | File Templates. } } System.out.println("server exiting"); //休眠60秒后就退出 System.exit(0); } }
2.用JaxWsServerfactorybean发布,需要独立的jetty包,包含的库有cxf-api-2.6.1.jar,cxf-rt-bindings-xml-2.6.1.jar,cxf-rt-core-2.6.1.jar,cxf-rt-frontend-jaxrs-2.6.1.jar,cxf-rt-transports-http-2.6.1.jar,cxf-rt-bindings-soap-2.6.1.jar,cxf-rt-databinding-jaxb-2.6.1.jar,cxf-rt-frontend-jaxws-2.6.1.jar,cxf-rt-frontend-simple-2.6.1.jar,cxf-rt-transports-http-jetty-2.6.1.jar,jetty-continuation-7.6.6.v20120903.jar,jetty-http-7.6.6.v20120903.jar,jetty-io-7.6.6.v20120903.jar,jetty-server-7.6.6.v20120903.jar,jetty-util-7.6.6.v20120903.jar,servlet-api-2.5.jar,wsdl4j-1.6.2.jar,xmlschema-core-2.0.2.jar
发布web service实现类
package com.sysware.app.service.impl; import com.sysware.app.service.inf.IDeployWebService; import org.apache.cxf.jaxws.JaxWsServerfactorybean; public class DeployWebServiceJaxwsImpl implements IDeployWebService { @Override public void deployService(String address) { GetInfoServiceImpl impl = new GetInfoServiceImpl(); JaxWsServerfactorybean factorybean = new JaxWsServerfactorybean(); factorybean.setAddress(address); factorybean.setServiceClass(GetInfoServiceImpl.class); factorybean.setServiceBean(impl); factorybean.create(); } }
将App类中的代码更改
private final static IDeployWebService deployWebServicePublishImpl = new DeployWebServiceJaxwsImpl();
(2)编写客户端程序
运行cxf里面bin目录下wsdl2java
E:\Source\Java\mysource\testcxfclient>E:\Source\Java\apache-cxf-2.6.2\bin\wsdl2java -p com.sysware.app -d e:\Source\Java\mysource\testcxfclient\src\main\java http://192.168.128.234:9111/getInfoService?wsdl
会产生一系列web service接口java文件
-p 指定其wsdl的命名空间,也就是要生成代码的包名
-d 指定要产生代码所在目录
主类
package com.sysware.app; import com.sysware.app.domain.RetInfo; import com.sysware.app.service.impl.GetInfoServiceImpl; import com.sysware.app.service.impl.GetWebServiceJaxwsImpl; import com.sysware.app.service.inf.IGetWebService; public class ClientApp { public static void main(String[] args) { String ip = ""; int port = 8080; if (args.length > 0) { ip = args[0]; if (args.length > 1) port = Integer.parseInt(args[1]); } String address = "http://" + ip + ":" + String.valueOf(port) + "/getInfoService"; IGetWebService getWebService = new GetWebServiceJaxwsImpl(); GetInfoServiceImpl getInfoService = getWebService.getService(address); if (null != getInfoService) { System.out.println("10 + 20 = " + Integer.toString(getInfoService.add(10,20))); RetInfo retInfo = getInfoService.getRetInfo("johnny",22); System.out.println("retInfo:" + retInfo.getName()); } } }
获取web service接口类
package com.sysware.app.service.inf; import com.sysware.app.service.impl.GetInfoServiceImpl; public interface IGetWebService { public GetInfoServiceImpl getService(String address); }1.通过JaxWsServerfactorybean调用
获取web service 接口实现类
package com.sysware.app.service.impl; import com.sysware.app.service.inf.IGetWebService; import org.apache.cxf.jaxws.JaxWsProxyfactorybean; public class GetWebServiceJaxwsImpl implements IGetWebService { @Override public GetInfoServiceImpl getService(String address) { JaxWsProxyfactorybean soapfactorybean = new JaxWsProxyfactorybean(); soapfactorybean.setAddress(address); soapfactorybean.setServiceClass(GetInfoServiceImpl.class); return (GetInfoServiceImpl) soapfactorybean.create(); } }2.通过标准JAX_WS API实现
package com.sysware.app.service.impl; import com.sysware.app.service.inf.IGetWebService; import java.net.MalformedURLException; import java.net.URL; public class GetWebServiceQNameImpl implements IGetWebService { @Override public GetInfoServiceImpl getService(String address) { try { GetInfoServiceImplService getInfoServiceImplService = new GetInfoServiceImplService(new URL(address + "?wsdl")); return getInfoServiceImplService.getGetInfoServiceImplPort(); } catch (MalformedURLException e) { e.printstacktrace(); return null; } } }GetInfoServiceImplService将由wsdl2java自动产生
主类更改如下
IGetWebService getWebService = new GetWebServiceQNameImpl();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。