WS服务调用

方法一:使用jws的高层封装,如:

package cxf.test;

import javax.xml.namespace.QName;

import javax.xml.ws.Service;

import javax.xml.ws.soap.SOAPBinding;

import cxf.test.HelloWorld; // necessary

public final class Client {

private static final QName SERVICE_NAME

= new QName("http://test.cxf/", "HelloWorld"); // 首参为接口实现类包名的反缀

private static final QName PORT_NAME

= new QName("http://test.cxf/", "HelloWorldPort");

private Client() { }

public static void main(String args[]) throws Exception {

Service service = Service.create(SERVICE_NAME);

// Endpoint Address

String endpointAddress = "http://localhost:8080/CXFTomcat/services/HelloWorld";

// Add a port to the Service

service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);

HelloWorld hw = service.getPort(HelloWorld.class);

System.out.println(hw.say("World"));

}

}

方法二:使用较下层的代码更加精确的控制程序的行为,如:

package cxf.test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import cxf.test.HelloWorld; // necessary

public final class Client {

private Client() { }

public static void main(String args[]) throws Exception {

JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();

factoryBean.getInInterceptors().add(new LoggingInInterceptor());(可选)

factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());(可选)

factoryBean.setServiceClass(cxf.test.HelloWorld.class);

factoryBean.setAddress("http://localhost:8080/CXFTomcat/services/HelloWorld");

HelloWorld client = (HelloWorld)factoryBean.create();

System.out.println(client.say("God"));

System.exit(0);

}

}

备注:LoggingInInterceptor和LoggingOutInterceptor是日志拦截器,用于输入和输出时显示日志。使用与否并不影响程序的行为。

方法三:使用Spring,例如:

package cxf.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cxf.test.HelloWorld; // necessary

public final class Client {

private Client() { }

public static void main(String args[]) throws Exception {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"cxf/test/client-beans.xml"});

HelloWorld client = (HelloWorld)context.getBean("client");

String response = client.say("Joe");

System.out.println("Response: " + response);

System.exit(0);

}

}

注意:要想使用Spring来完成,在cxf.test包中必须有client-beans.xml存在,内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:jaxws="http://cxf.apache.org/jaxws"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">

<bean id="client" class="cxf.test.HelloWorld"

factory-bean="clientFactory" factory-method="create"/>

<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">

<property name="serviceClass" value="cxf.test.HelloWorld"/>

<property name="address" value="http://localhost:8080/CXFTomcat/services/HelloWorld"/>

</bean>

</beans>

4.执行

Run As Java Application
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值