WebService

無缺格式見:https://www.zybuluo.com/Spancymath/note/1623555java

什麼是WebService?web

基於web的服務,使用Web(HTTP)方式,接收和響應外部系統的某種請求,從而實現遠程調用。ajax

名詞解釋

  1. XML: Extensible Markup Language - 擴展性標記語言
    用於傳輸格式化數據
  2. WSDL: WebService Description Language - Web服務描述語言
    經過XML形式說明服務在什麼地方
    經過XML形式說明服務提供什麼樣的方法
  3. SOAP: Simple Object Access Protocol - 簡單對象訪問協議
    SOAP = 在HTTP的基礎上+XML數據
    組成:Envelope(必須)+ Headers(可選)+ Body(必須)

在Java項目中發佈WS服務

  1. 在類上添加@WebService註解
  2. 經過EndPoint發佈一個WebService
    a.給類添加@WebService註解後,類中的全部非靜態、非final方法都將會對外公佈
    b.若是不但願某個方法不對外公開,能夠在方法上添加@WebService(Exclude=true)
//一、添加註解
@WebService
public class OneService {
   //二、至少包含一個能夠對外公開的服務
   public String sayHello(String name){
   System.err.println("invoke "+name);
   return "Hello "+name;
   }
   public static void main(String[] args) {
   //三、第一個參數稱爲Binding即綁定地址,
   //第二個參數是實現者,即誰提供服務
   Endpoint.publish("http://localhost:9999/one", new OneService());
   }
}
//4.在IE地址欄輸入如下地址訪問說明文件:
http://localhost:9999/one?wsdl
  1. 經過wsimport生成本地代碼spring

    經常使用參數爲:
    -d <目錄> - 將生成.class文件。默認參數。
    -s <目錄> - 將生成.java文件。
    -p <生成的新包名> -將生成的類,放於指定的包下。
    (wsdlurl) - http://server:port/service?wsdl,必須的參數。
    示例:
    C:/> wsimport –s . http://192.168.0.100/one?wsdl apache

  2. 總結
    WebService和Web服務器的區別:WebService是Web服務器的應用,Web服務器是WebService運行所必須的容器。
    WebService其內部是經過Socket實現的。編程

WebService的特色:服務器

  • WebService經過HTTP POST方式接受客戶的請求
  • WebService與客戶端之間通常使用SOAP協議傳輸XML數據.
  • 它自己就是爲了跨平臺或跨語言而設計的。

客戶端調用WebService

  1. 經過wsimport生成客戶端代碼
  2. 經過客戶端編程的方式調用
  3. 經過ajax調用 (js+XML)
var xhr = new XMLHttpRequest();
           function sedAjax() {
               var url = "http://localhost:8081/hello";
               var nameText = document.getElementById("name").value;
               var request = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                   "<soap:Body><ns2:sayHi xmlns:ns2=\"http://server/\">" +
                   "<arg0>" + nameText + "</arg0></ns2:sayHi></soap:Body></soap:Envelope>";
               $.ajax({
                   url: url,
                   type: "post",
                   contentType: "application/soap+xml;charset=utf-8",
                   data: request,
                   success: function (data) {
                       alert($(data).find("return").text());
                   }
               })

           }
  1. 經過URLConnection調用

SOAP請求過程分析app

  • 使用get方式獲取wsdl文件,稱爲握手。
    對於JDK1.6生成的ws服務,因爲內部有一兩個配置文件,因此會發出兩次get請求。其餘的通常爲一次。
  • 用戶發出請求將使用post方式。
  • 服務器響應成功。

CXF

  1. 使用ServerFactoryBean發佈應用
  • 使用CXF發佈一個服務,與JDK6發佈一個服務徹底不一樣
    即便是不使用@WebService註解,同樣能夠發佈成功
    即便此類沒有對外公佈的方法同樣能夠發佈成功
public class CxfServer1 implements ICxfServer1 {

   public String sayHi(String name) {
       return "Hello " + name;
   }

   public static void main(String[] args) {
       //聲明實例,使用ServerFactoryBean發佈服務
       //使用CXF發佈一個服務,與JDK6發佈一個服務徹底不一樣
       //* 即便是不使用@WebService註解,同樣能夠發佈成功
       //* 即便此類沒有對外公佈的方法同樣能夠發佈成功
       ServerFactoryBean bean = new ServerFactoryBean();
       //綁定到發佈地址的端口
       bean.setAddress("http://localhost:8080/cxf");
       //設置服務接口,若是沒有接口,則爲本類
       bean.setServiceClass(ICxfServer1.class);
       //設置接口實現類,即服務類
       bean.setServiceBean(new CxfServer1());
       //發佈服務
       bean.create();

       System.err.println("啓動成功");
   }
}
  1. 使用JaxWsServerFactoryBean(建議使用此類)發佈應用
  • JaxWsServerFactoryBean是ServerFactoryBean的子類,也是功能擴展類。
  • 但在CXF的API文檔中沒有提供此類API,請經過查看源代碼的方式獲取此類的幫助。
  • 此類,必需要在被髮布爲服務的類上添加@WebService註解,若是不加註解,雖然不
    出錯,但也不會對外暴露任何方法。
    使用此類生成的wsdl文件更加規範。
public class HelloImpl implements IHello {
   @Override
   public String sayHi(String name) {
       System.out.println("syaHi called");
       return "Hello2 " + name;
   }

   public static void main(String[] args) {
       //使用jaxWs對其進行發佈
       JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
       //設置訪問地址
       bean.setAddress("http://localhost:8081/hello");
       //註冊服務接口
       bean.setServiceClass(IHello.class);
       //註冊服務實現類
       bean.setServiceBean(new HelloImpl());

       //添加消息攔截器
       bean.getInInterceptors().add(new LoggingInInterceptor());
       bean.getOutInterceptors().add(new LoggingOutInterceptor());

       //啓動
       bean.create();
       System.out.println("服務啓動完成...");
   }

WebService-CXF-Spring 基於web的cxf

  1. 因爲cxf的web項目已經集成了Spring因此,cxf的服務類都是在spring的配置文件中完成的。如下是步驟:
  • 第一步:創建一個web項目。
  • 第二步:準備全部jar包。將cxf_home\lib項目下的全部jar包所有copy到新項目的lib目錄下,裏面已經包含了spring3.0的jar包。
  • 第三步:在web.xml中配置cxf的核心servlet,CXFServlet。
<servlet>
       <servlet-name>cxf</servlet-name>
       <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
       <!--<init-param>
           <param-name>config-location</param-name>
           <param-value>classpath:cxf-servlet.xml</param-value>
       </init-param>-->
       <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
       <servlet-name>cxf</servlet-name>
       <url-pattern>/ws/*</url-pattern>
   </servlet-mapping>
  • 第四步:建立(最好是Copy)cxf-servlet.xml文件。這是一個spring的配置文件。
<?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"
      xmlns:soap="http://cxf.apache.org/bindings/soap"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://cxf.apache.org/bindings/soap
      http://cxf.apache.org/schemas/configuration/soap.xsd
      http://cxf.apache.org/jaxws
      http://cxf.apache.org/schemas/jaxws.xsd">
   <!-- 引入CXF Bean定義以下,早期的版本中使用 -->
   <import resource="classpath:META-INF/cxf/cxf.xml" />
   <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
   <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
   <!-- 簡單發佈,沒有接口 -->
   <jaxws:endpoint id="helloService" address="/hello" implementor="com.zhang.server1.HelloImpl">
       <!--客戶端請求的消息攔截器-->
       <jaxws:inInterceptors>
           <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
       </jaxws:inInterceptors>
       <!--服務端響應的消息攔截器-->
       <jaxws:outInterceptors>
           <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
       </jaxws:outInterceptors>

   </jaxws:endpoint>
相關文章
相關標籤/搜索