使用CXF实现WebService,并在客户端实现动态调用

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",serviceName="helloWorldService",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("张三")); 
    } 

 

转自:http://blog.csdn.net/gufeng672/article/details/9086265

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值