1. 程式人生 > >webservice服務端釋出和客戶端呼叫

webservice服務端釋出和客戶端呼叫

1.服務端和客戶端增加Maven依賴包,如下

        <!-- cxf方式webservice服務 -->

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.1.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.1.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.1.14</version>
        </dependency>

2、服務端示例

1)編寫HelloWorld 介面類 程式碼如下:

package net.cc.service;

import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * @author test
 * @create 2013-11-26下午10:21:13
 */
@WebService
public interface HelloWorld {

    @WebMethod(operationName="sayHello")

    public String sayHello(@WebParam(name = "userName") String userName);

}

2)編寫HelloWorldImpl實現類 如下:

package net.cc.service;

import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * @author test
 * @create 2013-11-26下午10:22:53
 */
@WebService(serviceName = "HelloWorld", endpointInterface="net.cc.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

    @Override
    public String sayHello(@WebParam(name = "userName") String userName) {
        // TODO Auto-generated method stub
        System.out.println("客戶端提交資訊: " + userName);
        return "say Hello " + userName;
    }
}

3)服務端釋出介面

方式一:xml方式,新增Spring-webservice.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/schemas/jaxws.xsd">

    <jaxws:endpoint id="ProjectManager" implementor="net.cc.service.HelloWorldImpl"
        address="http://192.168.1.105:7890/HelloWorld" />

</beans>

web專案,可在web.xml在載入Spring-webservice.xml,或者編寫監聽器載入,如下:

package net.cc.servlet;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.support.ClassPathXmlApplicationContext;


/**
 * @author test
 * @create 2013-11-26下午10:41:53
 */
public class myListener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("啟動Tomcat...");
        ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext(
                "/net/cc/service/spring-beans.xml");

    }

}

實現 ServletContextListener 目的是為了在Tomcat啟動時自動載入

使用 ClassPathXmlApplicationContext 去載入剛才寫的Spring-webservice.xml檔案

在當前專案中web.xml檔案  增加如下程式碼:

<listener>
  <listener-class>net.cc.servlet.myListener</listener-class>
</listener>

 方式二:java程式碼方式:

HelloWorld hello = new HelloWorldImpl();
String address = "http://192.168.1.105:7890/HelloWorld";
Endpoint.publish(address, hello);

注:介面埠7890必須與tomcat埠不同。

4)tomcat 啟動截圖:

通過訪問:http://192.168.1.105:7890/HelloWorld?wsdl 檢視介面是否釋出成功。

3、客戶端呼叫示例:

1)編寫相同的HelloWord介面:

package net.cc.service;

import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * @author test
 * @create 2013-11-26下午10:21:13
 */
@WebService(name="HelloWorld", targetNamespace="http://service.cc.net")
public interface HelloWorld {

    @WebMethod(operationName="sayHello")

    @WebResult(name="return", targetNamespace="http://service.cc.net")

    public String sayHello(@WebParam(name = "userName") String userName);

}

注:targetNamespace可通過http://192.168.1.105:7890/HelloWorld?wsdl找到,需與釋出介面中的targetNamespace一致。

2)呼叫服務端介面(通過代理工廠方式呼叫):       

         // 建立代理工廠
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();

        // 設定代理工廠屬性(服務類以及地址)
        factoryBean.setServiceClass(HelloWorld.class);
        factoryBean.setAddress("http://192.168.1.105:7890/HelloWorld");

        // 呼叫
        HelloWorld helloWorld = (HelloWorld) factoryBean.create();

       // 可配置請求超時設定

      configTimeout(helloWorld);

       String result = helloWorld.sayHello("測試");
       System.out.println(result);

     

    /**
     * 客戶端呼叫請求時超時設定
     * @param service
     */
    public static void configTimeout(Object service) {
        Client proxy = ClientProxy.getClient(service);
        HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(8*1000);//8S 請求時間
        policy.setReceiveTimeout(10*1000);//10S 連線時間
        conduit.setClient(policy);
    }