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

猜你喜欢

转载自linshow26.iteye.com/blog/2008382
今日推荐