WebService实现 1、 整个项目使用CXF来实现,在实现的过程中,在MyEclipse中对CXF下lib中的所有jar文件通过引入外部包来处理。 2、 在MyEclipse6.5中可以实现服务器端和客户端,但是客户端在使用wsdl进行动态调用的过程中总是报错,最后使用MyEclipse9.0实现了进行动态调用的实现。其中发生的错误如下: (1)Exception in thread "main" java.lang.LinkageError: 正在从引导类加载器加载 JAXB 2.1 API,但此 RI (来自jar:file:/D:/CXF/lib/jaxb-impl-2.2.5.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) 需要 2.2 API。请使用授权目录机制将 jaxb-api.jar 放在引导类加载器中。(请参阅 http://java.sun.com/j2se/1.6.0/docs/guide/standards/) 解决办法: 通过删除引入包中的jaxb-impl-2.2.5.jar文件可以解决。 (2)java.lang.IllegalArgumentException: Can not set final com.sun.tools.internal.xjc.reader.internalizer.InternalizationLogic field com.sun.tools.internal.xjc.reader.internalizer.DOMForest.logic to org.apache.cxf.endpoint.dynamic.DynamicclientFactory$1 解决办法: 此错误最后还是没有解决,不知道如何解决。 3、 在MyEclipse9.0实现时又发生如下错误: org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name {http://impl.cxf.com/}sayHello. 4、 原因在于@WebService说明中没有使用targetNamespace造成的。 @WebService(endpointInterface="com.cxf.interfaces.IHelloWorldService",serviceName="helloWorldService") 修改如下: @WebService(endpointInterface="com.cxf.interfaces.IHelloWorldService",serviceName="helloWorldService",targetNamespace="http://interfaces.cxf.com/") 5、 整个程序代码如下: (1)接口的定义: package com.cxf.interfaces; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface IHelloWorldService { String sayHello(@WebParam(name="username") String username); } (2)服务器端的实现: package com.cxf.impl; import javax.jws.WebService; import javax.xml.ws.Endpoint; import com.cxf.interfaces.IHelloWorldService; @WebService(endpointInterface="com.cxf.interfaces.IHelloWorldService",targetNamespace="http://interfaces.cxf.com/") public class Server implements IHelloWorldService{ public String sayHello(String username) { return "Hello,"+username; } public static void main(String[] args) { Server impl=new Server(); String address="http://127.0.0.1:9000/hello"; Endpoint.publish(address,impl); } } (3)客户端的实现: package com.cxf.client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicclientFactory; public class ClientFromWsdl { public static void main(String[] args) { try { JaxWsDynamicclientFactory dcf = JaxWsDynamicclientFactory.newInstance(); org.apache.cxf.endpoint.Client client = dcf.createClient("http://127.0.0.1:9000/hello?wsdl"); //sayHello 为接口中定义的方法名称 张三为传递的参数 返回一个Object数组 Object[] objects=client.invoke("sayHello","张三"); //输出调用结果 System.out.println(objects[0].toString()); } catch(Exception e) { e.printstacktrace(); } } } (4)客户端的实现(非动态的) package com.cxf.client; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsProxyfactorybean; import com.cxf.interfaces.IHelloWorldService; public class Client { public static void main(String[] args) { JaxWsProxyfactorybean factorybean=new JaxWsProxyfactorybean(); factorybean.getininterceptors().add(new LoggingInInterceptor()); factorybean.getoutInterceptors().add(new LoggingOutInterceptor()); factorybean.setServiceClass(IHelloWorldService.class); factorybean.setAddress("http://localhost:9000/hello"); IHelloWorldService impl=(IHelloWorldService) factorybean.create(); System.out.println(impl.sayHello("张三")); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。