1

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


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

1 创建maven工程

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

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.liubo</groupId>
    <artifactId>text-cxf-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.7.2</version>
                <configuration>
                    <forkMode>once</forkMode>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                    <systemProperties>
                        <property>
                            <name>net.sourceforge.cobertura.datafile</name>
                            <value>target/cobertura/cobertura.ser</value>
                        </property>
                    </systemProperties>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>2.7.3</version>
                <executions>
                    <execution>
                        <id>generate-sources</id>
                        <phase>generate-sources</phase>
                        <configuration>
                            <sourceRoot>${project.build.sourceDirectory}</sourceRoot>
                            <encoding>UTF-8</encoding>
                            <wsdlOptions>
                                <wsdlOption>
                                    <wsdl>http://localhost:8080/test-cxf/HelloWS?wsdl</wsdl>
                                </wsdlOption>
                            </wsdlOptions>
                        </configuration>
                        <goals>
                            <goal>wsdl2java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

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


我有切糕
863 声望45 粉丝

Java架构师,技术经理