Web Service 開發系列文章之二(一些SOAP相關的內容)


Web Service 學習第二期

1、對SOAP的觀察和理解

1.1、使用Eclipse的Web Service Explorer

選擇JavaEE視圖 ,如果沒有則選擇:windowàOpen Perspective à Other

 

 

 

 

點擊 和左邊的

 

輸入服務的URL

 

 

點擊go

 

 

點擊add 輸入參數

 

下面顯示soap消息

 

 

 

 

1.2、基於SOAP的通信方式和基於JAX-WS的通信方式

1.2.1、初步區別

 

基於JAX-WS

JAX-WS封裝了SOAP Java的API自動將Java代碼轉換成了SOAP消息然后將SOAP請求消息發送個服務器。Java將要通信的對象自動轉化成消息了。

 

 

基於SOAP的

其實也可以直接寫一個消息發送給服務器(下面紅框中的消息)

 

 

 

 

1.2.2、實施

新建Java項目 新建包

(1) 創建服務接口

package org.decarl.soap.service;

import javax.jws.WebParam;

import javax.jws.WebResult;

import javax.jws.WebService;

 

@WebService(targetNamespace="http://decarl.soap.org/webservice")

public interface IMyService {

    @WebResult(name="addResult")

    public int add(@WebParam(name="a") int a,

             @WebParam(name="b") int b);

}

注意:一個很重要的問題

此服務發布后的WSDL的targetNamespace依舊是根據包生成的命名空間 targetNamespace=http://service.soap.decarl.org/

 

當@WebService(targetNamespace="http://decarl.soap.org/webservice")中指定的targetNamespace與包名不同時會將WSDL分成兩個

 

在上圖中的紅框下面一句就是導入的自己在@WebService(targetNamespace="http://decarl.soap.org/webservice")

中指定的命名空間

 

 

 

 

 

(2) 創建服務接口實現類

 

package org.decarl.soap.service;

 

import javax.jws.WebService;

 

//指明服務端接口是誰

@WebService(endpointInterface="org.decarl.soap.service.IMyService")

public class MyServiceImpl implements IMyService {

 

    @Override

    public int add(int a, int b) {

        System.out.println("a + b = " + (a + b));

        return a + b;

    }

}

 

 

(3) 創建服務類

 

package org.decarl.soap.service;

 

import javax.xml.ws.Endpoint;

 

public class MyServer {

 

    public static void main(String[] args) {

        Endpoint.publish("http://localhost:8989/ms", new MyServiceImpl());

    }

}

 

 

 

 

1.2.3、SOAP結構

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM