webservice服務端發佈和客戶端調用(使用jetty作爲服務器)

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);
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章