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

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

转载于:https://my.oschina.net/LuciusLiu/blog/784858

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值