使用CXF实现WebService,并在客户端实现动态调用
发布时间:2020-12-17 00:18:09 所属栏目:安全 来源:网络整理
导读:WebService实现 1、 整个项目使用CXF来实现,在实现的过程中,在MyEclipse中对CXF下lib中的所有jar文件通过引入外部包来处理。 2、 在MyEclipse6.5中可以实现服务器端和客户端,但是客户端在使用wsdl进行动态调用的过程中总是报错,最后使用MyEclipse9.0实现
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("张三")); ??? } }
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |