WebService

WebService簡單使用

1.WebService是什麼?
WebService是一個基於Web的服務,使服務端的資源能夠被客戶端訪問。
實質上WebService是一種跨編程語言和操作系統平臺的遠程調用技術或者說一種規範。

剛開始接觸WebService時,會感到迷惑。WebService和Struts2、SpringMVC似乎在功能上都差不多。 但其實還是有本質差別的,Struts2和SpringMVC都是基於框架的,屬於MVC中的Controller,是封裝了針對傳統Http請求的處理,對客戶端的請求沒有太多要求,按照正常發送Http請求即可。

而WebService更多的是一種概念。是基於語言的基礎上的,不同的語言有不同的實現,傳統的WebService是基於Http + xml片段的,消息傳遞是封裝在xml中,採用wsdl描述請求與相應數據,採用soap協議交換數據。

在開發過程中,可能還會產生一個疑問,看起來WebService要實現的功能,我使用Struts2和SpringMVC的的接口也都能實現啊,爲什麼還要用?
這個問題,我猜如果以前都使用的SpringMVC這種框架,現在要來使用WebService開發,絕對不是自己選擇的吧。大概率都是別人要求的,技術妥協。。。。。

個人感覺WebService用起來不是很爽,可能現在WebService的使用更多的是考慮到對.net這種開發的支持吧。

目前WebService大致分爲兩種,Soap WebService 與 Restful WebService。

相對而言,SOAP協議屬於複雜的、重量級的協議,當前隨着Web2.0的興起,表述性狀態轉移(Representational State Transfer,REST)逐步成爲一個流行的架構風格。REST是一種輕量級的Web Service架構風格,其實現和操作比SOAP和XML-RPC更爲簡潔,可以完全通過HTTP協議實現,還可以利用緩存Cache來提高響應速度,性能、效率和易用性上都優於SOAP協議。REST架構對資源的操作包括獲取、創建、修改和刪除資源的操作正好對應HTTP協議提供的GET、POST、PUT和DELETE方法,這種針對網絡應用的設計和開發方式,可以降低開發的複雜性,提高系統的可伸縮性。REST架構尤其適用於完全無狀態的CRUD(Create、Read、Update、Delete,創建、讀取、更新、刪除)操作。

基於REST的軟件體系結構風格(Software Architecture Style)稱之爲面向資源體系架構(Resource-oriented Architecture,ROA)。按照REST原則設計的軟件、體系結構,通常被稱爲“REST式的”(RESTful)。

服務器端採用J2EE,客戶端採用JSP、Flex、JavaFX、AIR等可以直接調用Servlet,其他的實現技術基本上不能直接調用,但是無論是那種客戶端,對於基於SOAP的Web服務或者基於RESTful Web服務務都是支持的,如AJAX的 XMLHttpRequest、Flex的HTTPService等。

2.WebService能做什麼?
WebService能夠做到跨語言、跨操作系統平臺進行遠程調用技術。

3.WebService基本用法
3.1 簡單的 WebService 服務發佈
3.1.1 服務定義

@WebService
public interface HelloWS {

    @WebMethod
    public String sayHello(String name);
}  

3.1.2 服務實現

@WebService
public class HelloWSImpl implements HelloWS {
    @Override
    public String sayHello(String name) {
        System.out.println("server sayHello: " + name);
        return "Hello " + name;
    }
}  

3.1.3 服務發佈

public class ServerTest {

    public static void main(String[] args) {
        String address = "http://192.168.1.139:8989/wsDemo/hellows";
        Endpoint.publish(address, new HelloWSImpl());
        System.out.println("發佈成功");
    }
}  

3.2 簡單的客戶端訪問服務端
3.2.1 步驟一
根據wsdl文件(http://192.168.1.139:8989/wsDemo/hellows?wsdl)生成客戶端文件,我這裏使用的IDEA,所以可以直接在文件目錄上右鍵
步驟一

3.2.2 步驟二
步驟二

3.2.3 步驟三
步驟三

3.2.4 調用生成的客戶端代碼,訪問服務端方法

public class ClientTest {

    public static void main(String[] args) {
        HelloWSImplService factory = new HelloWSImplService();
        HelloWSImpl helloWSImpl = factory.getHelloWSImplPort();
        System.out.println(helloWSImpl.getClass());

        String resulr = helloWSImpl.sayHello("Zard");
        System.out.println(resulr);

    }
}  

4.WebService的CXF框架使用
4.1 導入CXF的依賴jar包,我這裏使用的maven

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>4.0.6.RELEASE</version>
</dependency>

<!-- cxf -->

<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-frontend-jaxws</artifactId>
  <version>${cxf.version}</version>
</dependency>

<dependency>
  <groupId>javax.ws.rs</groupId>
  <artifactId>javax.ws.rs-api</artifactId>
  <version>2.0.1</version>
</dependency>

<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-frontend-jaxrs</artifactId>
  <version>${cxf.version}</version>
</dependency>

<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-jaxrs</artifactId>
  <version>1.9.13</version>
</dependency>

<!-- 使用CXF時,採用JaxWsServerFactoryBean發佈服務時需要 -->
<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-transports-http-jetty</artifactId>
  <version>3.1.6</version>
</dependency>

<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-transports-http</artifactId>
  <version>3.1.8</version>
</dependency>

4.2 服務端定義與實現如3.1.1、3.1.2,服務端發佈如下(也可以採用原始的Endpoint來發布)

public class Service {

    public static void main(String[] args) {
        JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
        factoryBean.setServiceClass(OrderServiceImpl.class);
        factoryBean.setAddress("http://localhost/services/orderService");
        Server server = factoryBean.create();
        server.start();
        System.out.println("服務啓動成功");
    }
}  

4.3 客戶端從服務端獲取數據如下,這種情況需要客戶端頂一個與服務端定義一樣的藉口

public class ClientForCXF {

    public static OrderService getInterFace(){
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        factoryBean.setServiceClass(OrderService.class);
        factoryBean.setAddress("http://localhost/services/orderService");
        return (OrderService) factoryBean.create();
    }

    public static void main(String[] args) {
        OrderService service = getInterFace();
        Order order = service.getOrder(5);
        System.out.println(order.toString());
    }
}

5.WebService結合Spring的使用
5.1 配置Web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name></display-name>
    <!-- Spring啓動 -->
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- CXFServlet -->
    <servlet>
      <description>Apache CXF Endpoint</description>
      <display-name>cxf</display-name>
      <servlet-name>cxf</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
      <servlet-name>cxf</servlet-name>
      <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

    <filter>
      <filter-name>encodingFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
      </init-param>
      <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>encodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

5.2 配置applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd
       http://cxf.apache.org/jaxrs
       http://cxf.apache.org/schemas/jaxrs.xsd">

    <context:component-scan base-package="rest" use-default-filters="true"/>
    <context:component-scan base-package="soap" use-default-filters="true"/>

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <!-- soap webService -->
    <jaxws:endpoint id="orderService" implementorClass="soap.OrderServiceImpl" address="/orderService"/>

    <!-- restful webService -->
    <jaxrs:server id="rs1" address="/rs">
        <jaxrs:serviceBeans>
            <ref bean="restComponent"/>
        </jaxrs:serviceBeans>
        <jaxrs:providers>
            <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
        </jaxrs:providers>
    </jaxrs:server>
</beans> 

5.3 配置Tomcat
添加Tomcat容器,然後把項目發佈上去。

6.WebService、Spring、Restful的結合使用
在applicationContext中已經設置了Restful

<!-- restful webService -->
<jaxrs:server id="rs1" address="/rs">
    <jaxrs:serviceBeans>
        <ref bean="restComponent"/>
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
    </jaxrs:providers>
</jaxrs:server>  

cxf Restful 服務端的定義:

@Component("restComponent")
public class RestfulTest1 {

    @GET
    @Path("/getOrder")
    @Produces({MediaType.APPLICATION_JSON})
    public Order getOrder(){
        return new Order(1024L, "Zard", "蜂蜜柚子茶", 2);
    }
}

結合web.xml,applicationContext.xml與服務端,Restful的最終訪問路徑爲:

http://localhost/services/rs/getOrder  

返回結果爲:

{
    "orderId": 1024,
    "customer": "Zard",
    "itemName": "蜂蜜柚子茶",
    "itemCount": 2
}  

詳細我已經上傳到Github上了:
不使用CXF的基礎情況
使用CXF集成Spring以及Restful實例,也包含了原始的Soap

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