學習隨筆-Java WebService,-javawebservice

來源:互聯網
上載者:User

學習隨筆-Java WebService,-javawebservice

webService 可以將應用程式轉換成網路應用程式。是簡單的可共同操作的訊息收發架構。

基本的webService平台是 XML 和 HTTP。

      HTTP 是最常用的互連網協議;

      XML 是 webService 的基礎,因為可以用於不同平台和程式設計語言之間。

webService平台的元素:

       SOAP(簡易對象訪問協議);

       UDDI(通用描述、發現、整合);

       WSDL(web service 描述語言);

webService 有兩種類型的應用

      1)可重複使用的應用程式組件:有些功能是不同應用程式常常用到的,比如:天氣預報。webService可以把應用程式組件當做服務來提供;

      2)串連現有軟體:通過為不同程式提供一種連結其資料的途徑,解決協同辦公的問題。

簡單程式:

建立一個service 的 web service project工程 :

package com.service;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService   //將類標註為webService
public class Test {
    
    @WebMethod  //標註用戶端調用的方法
    public String getString (String name){
        return name.toUpperCase();
    }
    public static void main(String[] args) {

   //發布到服務端 參數1:網路通訊協定+ip+連接埠號碼(不能被佔用)+上下文根+調用方法  參數2:new一個本類的執行個體
        Endpoint.publish("http://localhost:8085/Test", new Test());

   //標誌著編譯成功結束 可不寫
        System.out.println("OK");
    }
}

//設定檔我現在還沒有搞明白  如果不配配置測試地址就無響應

配置一個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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 註冊組件掃描器 -->
    <context:component-scan base-package="com.service" />
</beans>

測試地址:http://localhost:8085/Test?wsdl

利用 webService soapUI 4.5.2軟體 連結http://localhost:8085/service/Test 自動產生應用服務

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:getString>
         <!--Optional:-->
         <arg0>yyyy</arg0>  //<arg0>?</arg0> 原始顯示是? 為需求參數
      </ser:getString>
   </soapenv:Body>
</soapenv:Envelope>

 

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getStringResponse xmlns:ns2="http://service.com/">
         <return>YYYY</return>  //<return>?</return> 原始顯示是? 為處理結果
      </ns2:getStringResponse>
   </S:Body>
</S:Envelope>

 

2、命令提示視窗執行產生命令。

先建立一個client 的web service project 工程

開啟 命令視窗 輸入

格式:wsimport -s "src目錄" -p “產生類所在包名” -keep “wsdl發布地址”

    1)"src目錄"地址不可含空格

    2)“wsdl發布地址”不要漏了“?wsdl”

效果:

  wsimport -s E:\WorkSpace\maven\client\src  -p com.client -keep http://localhost:8085/Test?wsdl
  正在解析 WSDL...
  正在產生代碼...
  正在編譯代碼...

重新整理項目就可以看到產生的client代碼

但是client 用戶端代碼會有亂碼問題目前還沒有解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.