XFire是一个高效的轻量级web service框架,可以快速搭建web service应用,并且提供了多种方式构建web service客户端。这里介绍XFire构建web service客户端的五种方式的详细攻略。
方式一:使用Java代码手动构建客户端
使用Java代码手动构建客户端,需要借助XFIRE的相关API(包括Annotation,Client等)。步骤如下:
- 新建Java项目,导入xFire相关的jar包
在项目中导入xfire-core.jar、xfire-aegis.jar等xfire相关jar包。
- 定义API接口
定义API接口,使用Annotation标注相关参数,在参数中设置@WebParam(name=””)的值表示参数名称,例如:
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(name="MathService",
portName = "MathServicePort",
serviceName = "MathService",
targetNamespace = "http://xfire.demo.com/MathService")
public interface MathService {
float add(@WebParam(name="value1")float value1, @WebParam(name="value2")float value2);
float substract(float value1, float value2);
}
- 生成客户端代理类
使用XFire的ClientFactory对象构建Service客户端代理类,例如:
Class serviceClass = MathService.class;
String serviceUrl = "http://xfire.demo.com/MathService";
ClientProxyFactoryBean clientFactoryBean = new ClientProxyFactoryBean();
clientFactoryBean.setServiceClass(serviceClass);
clientFactoryBean.setAddress(serviceUrl);
MathService service = (MathService) clientFactoryBean.create();
- 调用接口
调用生成的客户端代理类中的MathService接口方法即可。例如:
float result = service.add(1.0f, 2.0f);
方式二:使用Spring配置文件构建客户端
使用Spring配置文件构建客户端,需要在Spring配置文件中进行相关配置。步骤如下:
- 新建Spring项目,导入xFire和Spring相关的jar包
在Spring项目中导入xfire-core.jar、xfire-aegis.jar、spring-beans.jar、spring-context.jar等相关jar包。
- 定义API接口
同方式一中的步骤。
- 生成客户端代理类
使用Spring配置文件中的jaxws:client标签配置服务接口和Endpoint地址,例如:
<jaxws:client id="mathService" serviceClass="com.demo.xfire.MathService" address="http://localhost:8080/MathService">
</jaxws:client>
在Spring配置文件中定义服务的引用,例如:
<bean id="mathServiceClient" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="com.demo.xfire.MathService"/>
<property name="wsdlDocumentUrl" value="http://localhost:8080/MathService?wsdl"/>
<property name="serviceName" value="MathService"/>
<property name="portName" value="MathServicePort"/>
<property name="namespaceUri" value="http://xfire.demo.com/MathService"/>
</bean>
其中,serviceInterface属性指定接口类名,wsdlDocumentUrl属性指定服务的WSDL地址,serviceName和portName属性指定服务的名称和端口,namespaceUri属性指定服务的命名空间。
- 调用接口
同方式一中的步骤。
方式三:使用命令行工具构建客户端
使用XFire提供的命令行工具xfire.bat或xfire.sh构建客户端,具体步骤如下:
- 在命令行上执行以下命令:
xfire.bat genclient http://localhost:8080/MathService?wsdl -p com.demo.xfire -d src
其中,url参数指定服务的WSDL地址,-p参数指定生成客户端代码的包名,-d参数指定生成代码的目录。
- 生成客户端代码
在执行完上述命令后,会在指定的目录中生成服务接口的客户端代码,例如:
public interface MathServicePortType extends java.rmi.Remote {
float add(float value1, float value2) throws java.rmi.RemoteException;
float substract(float value1, float value2) throws java.rmi.RemoteException;
}
public class MathServiceClient {
private static final String ENDPOINT_ADDRESS = "http://localhost:8080/MathService";
private MathServicePortType service;
public MathServiceClient() throws MalformedURLException, ServiceException {
URL endpointUrl = new URL(ENDPOINT_ADDRESS);
QName serviceName = new QName("http://xfire.demo.com/MathService", "MathService");
Service service = Service.create(endpointUrl, serviceName);
this.service = service.getPort(MathServicePortType.class);
}
public float add(float value1, float value2) throws java.rmi.RemoteException {
return service.add(value1, value2);
}
public float substract(float value1, float value2) throws java.rmi.RemoteException {
return service.substract(value1, value2);
}
public static void main(String[] args) throws Exception {
MathServiceClient client = new MathServiceClient();
float result = client.add(1.0f, 2.0f);
System.out.println(result);
}
}
- 调用接口
在生成的客户端代码中调用服务接口即可。例如:
MathServiceClient client = new MathServiceClient();
float result = client.add(1.0f, 2.0f);
方式四:使用WSDL2Java命令行工具构建客户端
使用Apache CXF提供的命令行工具WSDL2Java构建客户端,具体步骤如下:
- 在命令行上执行以下命令:
wsdl2java -impl -ant http://localhost:8080/MathService?wsdl
其中,-impl参数表示是否生成实现类,-ant参数表示生成的构建文件格式为ANT风格。
- 生成客户端代码
在执行完上述命令后,会在指定的目录中生成服务接口的客户端代码,包括接口类、服务类以及相关的工具类,例如:
public interface MathService {
float add(float value1, float value2);
float substract(float value1, float value2);
}
public class MathServiceClient {
private static final QName SERVICE_NAME = new QName("http://xfire.demo.com/MathService", "MathService");
private static final QName PORT_NAME = new QName("http://xfire.demo.com/MathService", "MathServicePort");
private MathService service;
public MathServiceClient(URL wsdlUrl) {
Service service = Service.create(wsdlUrl, SERVICE_NAME);
this.service = service.getPort(PORT_NAME, MathService.class);
}
public float add(float value1, float value2) {
return service.add(value1, value2);
}
public float substract(float value1, float value2) {
return service.substract(value1, value2);
}
public static void main(String[] args) throws Exception {
URL wsdlUrl = new URL("http://localhost:8080/MathService?wsdl");
MathServiceClient client = new MathServiceClient(wsdlUrl);
float result = client.add(1.0f, 2.0f);
System.out.println(result);
}
}
- 调用接口
在生成的客户端代码中调用服务接口即可。例如:
URL wsdlUrl = new URL("http://localhost:8080/MathService?wsdl");
MathServiceClient client = new MathServiceClient(wsdlUrl);
float result = client.add(1.0f, 2.0f);
方式五:使用Maven插件构建客户端
使用Apache CXF提供的Maven插件构建客户端,具体步骤如下:
- 在项目中添加Maven依赖
添加以下依赖:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
- 在Maven POM文件中配置插件
在build->plugins节点下添加以下插件:
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<wsdlOptions>
<wsdlOption>
<wsdl>${project.basedir}/src/main/resources/MathService.wsdl</wsdl>
<wsdlLocation>classpath:/MathService.wsdl</wsdlLocation>
<bindingFiles>
<bindingFile>${project.basedir}/src/main/resources/MathService-bindings.xml</bindingFile>
</bindingFiles>
<extraargs>
<extraarg>-client</extraarg>
<extraarg>-verbose</extraarg>
<extraarg>-p</extraarg>
<extraarg>com.demo.xfire</extraarg>
<extraarg>-d</extraarg>
<extraarg>${project.build.directory}/generated-sources/cxf</extraarg>
</extraargs>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
其中,wsdl节点指定服务的WSDL文件所在路径,wsdlLocation节点指定WSDL文件在项目中的位置,bindingFiles节点指定自定义绑定文件的位置,extraargs节点指定额外的参数,例如客户端包名和生成代码的路径。
- 生成客户端代码
在Maven项目的根目录上执行以下命令:
mvn generate-sources
即可生成服务接口的客户端代码。
- 调用接口
在生成的客户端代码中调用服务接口即可。例如:
MathService service = new MathService();
float result = service.add(1.0f, 2.0f);
System.out.println(result);
示例
以方式二为例,在Spring配置文件中配置MathService客户端以及引用:
<jaxws:client id="mathService" serviceClass="com.demo.xfire.MathService" address="http://localhost:8080/MathService">
</jaxws:client>
<bean id="mathServiceClient" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="com.demo.xfire.MathService"/>
<property name="wsdlDocumentUrl" value="http://localhost:8080/MathService?wsdl"/>
<property name="serviceName" value="MathService"/>
<property name="portName" value="MathServicePort"/>
<property name="namespaceUri" value="http://xfire.demo.com/MathService"/>
</bean>
在Java程序中调用MathService客户端:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-client.xml");
MathService mathService = (MathService)context.getBean("mathServiceClient");
float result = mathService.add(1.0f, 2.0f);
System.out.println(result);
}
运行程序即可输出3.0的结果。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:XFire构建web service客户端的五种方式 - Python技术站