使用cxf-codegen-plugin实现WebServices客户端

WebServices服务搭建参见:使用CXF搭建WebServices服务端


使用cxf-codegen-plugin实现WebServices客户端

1 创建maven工程

添加cxf-codegen-plugin,在选项中添加wsdl地址


    4.0.0
    com.liubo
    text-cxf-client
    0.0.1-SNAPSHOT

    
        UTF-8
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.3.2
                
                    1.6
                    1.6
                    UTF-8
                
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                2.7.2
                
                    once
                    -Dfile.encoding=UTF-8
                    
                        
                            net.sourceforge.cobertura.datafile
                            target/cobertura/cobertura.ser
                        
                    
                
            
            
                org.apache.cxf
                cxf-codegen-plugin
                2.7.3
                
                    
                        generate-sources
                        generate-sources
                        
                            ${project.build.sourceDirectory}
                            UTF-8
                            
                                
                                    http://localhost:8080/test-cxf/HelloWS?wsdl
                                
                            
                        
                        
                            wsdl2java
                        
                    
                
            
        
    

2 执行maven构建(或自动构建),maven会自动生成相应的WebServices客户端代码

例如HelloWebService

package com.liubo.test.cxf.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by Apache CXF 2.7.3
 * 2016-04-28T18:53:56.903+08:00
 * Generated source version: 2.7.3
 * 
 */
@WebService(targetNamespace = "http://service.cxf.test.liubo.com/", name = "HelloWebService")
@XmlSeeAlso({ObjectFactory.class})
public interface HelloWebService {

    @WebResult(name = "return", targetNamespace = "")
    @RequestWrapper(localName = "sayHello", targetNamespace = "http://service.cxf.test.liubo.com/", className = "com.liubo.test.cxf.service.SayHello")
    @WebMethod
    @ResponseWrapper(localName = "sayHelloResponse", targetNamespace = "http://service.cxf.test.liubo.com/", className = "com.liubo.test.cxf.service.SayHelloResponse")
    public java.lang.String sayHello(
        @WebParam(name = "text", targetNamespace = "")
        java.lang.String text
    );
}

3 调用WebService,如下

package com.liubo.test.cxf.client;

import com.liubo.test.cxf.service.HelloWebService;
import com.liubo.test.cxf.serviceimpl.HelloWS;

public class Client {

    public static void main(String[] args) {
        
        HelloWS factory = new HelloWS();

        HelloWebService helloWebService = factory.getHelloWebServiceImplPort();

        System.out.println(helloWebService.sayHello("Libra"));
    }
}

执行成功,得到如下结果

hello Libra, welcome to the real world

你可能感兴趣的:(webservice,java,cxf)