cxf 创建webservice
参见文章1和文章2 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 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)编写客户端程序 E:SourceJavamysourcetestcxfclient>E:SourceJavaapache-cxf-2.6.2binwsdl2java -p com.sysware.app -d e:SourceJavamysourcetestcxfclientsrcmainjava http://192.168.128.234:9111/getInfoService?wsdl 会产生一系列web service接口java文件 主类 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(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |