天天看點

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

Manual Instruction Document

Web Service

JAX-WS & JAX-RS

Author: Liu Xiang

Date: 2018/01/12

1. Summary....................................................................................................................... 1

2. JAX-WS.......................................................................................................................... 1

2.1 The common interface..................................................................................... 1

2.2 The functions.................................................................................................. 1

2.3 To call common JAX-WS.................................................................................... 4

2.4 To call private JAX-WS which released by ourself................................................. 7

2.5 Other common JAX-WS.................................................................................. 10

3. JAX-RS......................................................................................................................... 12

3.1 The common interface................................................................................... 12

3.2 The functions................................................................................................ 12

3.3 To call common JAX-RS................................................................................... 15

3.4 To call private JAX-RS which released by ourself................................................. 18

3.5 Other common JAX-RS................................................................................... 18

1. Summary

There are twotypes of Web Service.

JAX-WS, java apifor xml-based webservice.

JAX-RS, java apifor RESTful webservice.

2. JAX-WS

2.1 Thecommon interface

Jar name WsClient.jar
Interface

package com.cn;

publicinterface WsClient {

public String getResult(StringapplyKey, Stringtype, String...message);

}

Parameters
Type Name Description
String applyKey User's Key, user's verification
String type The provided Web Service type name
String... message The input parameters
Return
Type Description
String The Web Service calling result

2.2The functions

2.2.1 Validations

2.2.1.2 CheckapplyKey

ToDo

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS
2.2.1.2 Checktype

Check resource/resource.properties which key = support.type to verify if theincoming type is support or not.

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

2.2.2 Prepare Web ServiceRequest

Use the SOAP 1.2 templates in "/com/cn/resource/request/template/"to prepare the request message.

For example:

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

2.2.3 Get Web Service Response

To call the related WSDL(Web Services DescriptionLanguage) URL, get the response.

For example:

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

2.2.4 Parse the response

Use the related key word to get result node.

For example:

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

2.3 Tocall common JAX-WS

2.3.1 Validations

Pass.

2.3.2 To call the interface

Use below code to call 2.1 common method, to get"蘇州"weather.

     WsClientwsClient =new WsClientImpl();

     System.out.println(wsClient.getResult("","weather","蘇州",""));

2.3.3 Send the request SOAP 1.2request

<?xmlversion="1.0"encoding="utf-8"?>

<soap12:Envelopexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">

 <soap12:Body>

   <getWeatherxmlns="http://WebXml.com.cn/">

     <theCityCode>蘇州</theCityCode>

     <theUserID></theUserID>

   </getWeather>

 </soap12:Body>

</soap12:Envelope>

2.3.4 Get the response

<?xmlversion="1.0"encoding="utf-8"?>

<soap:Envelopexmlns:soap="http://www.w3.org/2003/05/soap-envelope"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 <soap:Body>

   <getWeatherResponsexmlns="http://WebXml.com.cn/">

     <getWeatherResult>

        <string>江蘇蘇州</string>

        <string>蘇州</string>

        <string>2004</string>

        <string>2018/01/1210:26:03</string>

        <string>今日天氣實況:氣溫:0℃;風向/風力:北風 2級;濕度:31%</string>

        <string>紫外線強度:中等。空氣品質:中。</string>

        <string>紫外線指數:中等,塗擦SPF大于15、PA+防曬護膚品。健臻·血糖指數:易波動,血糖易波動,注意監測。感冒指數:較易發,天涼溫差大,适當增減衣服。穿衣指數:冷,建議着棉衣加羊毛衫等冬季服裝。洗車指數:較适宜,無雨且風力較小,易保持清潔度。空氣污染指數:中,易感人群應适當減少室外活動。</string>

        <string>1月12日晴</string>

        <string>-3℃/5℃</string>

        <string>西北風轉東南風小于3級</string>

        <string>0.gif</string>

        <string>0.gif</string>

        <string>1月13日晴轉多雲</string>

        <string>-1℃/7℃</string>

        <string>東南風3-4級</string>

        <string>0.gif</string>

        <string>1.gif</string>

        <string>1月14日多雲</string>

        <string>3℃/11℃</string>

        <string>東南風3-4級</string>

        <string>1.gif</string>

        <string>1.gif</string>

        <string>1月15日陰</string>

        <string>9℃/15℃</string>

        <string>東南風3-4級</string>

        <string>2.gif</string>

        <string>2.gif</string>

        <string>1月16日小雨</string>

        <string>7℃/16℃</string>

        <string>西北風3-4級</string>

        <string>7.gif</string>

        <string>7.gif</string>

     </getWeatherResult>

   </getWeatherResponse>

 </soap:Body>

</soap:Envelope>

2.3.5 Parse the response

江蘇蘇州

        蘇州

        2004

        2018/01/12 10:26:03

        今日天氣實況:氣溫:0℃;風向/風力:北風 2級;濕度:31%

        紫外線強度:中等。空氣品質:中。

        紫外線指數:中等,塗擦SPF大于15、PA+防曬護膚品。健臻·血糖指數:易波動,血糖易波動,注意監測。感冒指數:較易發,天涼溫差大,适當增減衣服。穿衣指數:冷,建議着棉衣加羊毛衫等冬季服裝。洗車指數:較适宜,無雨且風力較小,易保持清潔度。空氣污染指數:中,易感人群應适當減少室外活動。

        1月12日晴

        -3℃/5℃

        西北風轉東南風小于3級

        0.gif

        0.gif

        1月13日晴轉多雲

        -1℃/7℃

        東南風3-4級

        0.gif

        1.gif

        1月14日多雲

        3℃/11℃

        東南風3-4級

        1.gif

        1.gif

        1月15日陰

        9℃/15℃

        東南風3-4級

        2.gif

        2.gif

        1月16日小雨

        7℃/16℃

        西北風3-4級

        7.gif

        7.gif

2.4 Tocall private JAX-WS which released by ourself

2.4.1 Validations

Pass.

2.4.2 Release private JAX-WS

2.4.2.1 New project HelloFriend

 1. New java projectHelloFriend, New class MyService.java

package service;

publicclass MyService {

   public String getGreeting(Stringname)

   {

        return"您好 "+name;

   }

}

2. New META-INF folder and services.xml

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

services.xml

Service name is "myService", useRPC method

<servicename="myService">

   <description>

        WebService例子

   </description>

   <parametername="ServiceClass">

        service.MyService

   </parameter>

   <operationname="getGreeting">

       <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>

   </operation>

</service>

3. Export the project to jar

      jar cvf ws.aar .

2.4.2.2 Use axis2 to releaseit to tomcat

1. Move 2.4.1.1 step 3 jar \apache-tomcat-9.0.2\webapps\axis2\WEB-INF\services

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

2. Startup tomcat

3. Visit the private wsdl

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

2.4.3 To call the interface

        WsClientwsClient =new WsClientImpl();

         System.out.println(wsClient.getResult("","myservice","劉大人"));

2.4.4 Send the request SOAP 1.2request

<?xmlversion="1.0"encoding="utf-8"?>

<soap12:Envelopexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">

 <soap12:Body>

   <getGreetingxmlns="http://service">

    <name>劉大人</name>

   </getGreeting>

 </soap12:Body>

</soap12:Envelope>

2.4.5 Get the response

<?xmlversion="1.0"encoding="utf-8"?>

<soapenv:Envelopexmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">

 <soapenv:Header/>

 <soapenv:Body>

   <ns:getGreetingResponsexmlns:ns="http://service">

     <ns:return>您好劉大人</ns:return>

   </ns:getGreetingResponse>

 </soapenv:Body>

</soapenv:Envelope>

2.4.6 Parse the response

您好劉大人

2.5 Othercommon JAX-WS

2.5.1 Attribution of mobilephones

        WsClientwsClient =new WsClientImpl();

         wsClient.getResult("","mobile","18662638888","");

                  ==>

        18662638888:江蘇蘇州江蘇聯通GSM卡

2.5.2 Flight inquiries

                  WsClientwsClient =new WsClientImpl();

         wsClient.getResult("","airline","上海","北京","2018-01-12","");

                  ==>

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

 2.5.3 Chinese/Englishtranslations

        WsClientwsClient =new WsClientImpl();

         wsClient.getResult("","englishswpchinese","airline","");

                  ==>

airline

             'e?lain

             adj. (飛機)航線的;n. (飛機的)航線,航空公司

             65562.mp3

             However short the journey is, you always get something to eat on thisairline.

             不管航程多麽短,這一班機上都有些吃的.           

             That was the worst airline disaster in history.

             那是曆史上最嚴重的空難。           

             The timetable is obtainable post-free from the airline office.

             航班時刻表可以向航空公司免費索取。

        WsClientwsClient =new WsClientImpl();

         wsClient.getResult("","englishswpchinese","蘇州","");

                  ==>

                            蘇州        

             Suzhou (city)   

             soochow pladj.

             Suchou

             Soochow

             對不起,打擾了,請問坐火車去蘇州該乘什麼車?

             Excuse me, how can I go to suzhou by train?           

             一個孕婦把他從蘇州河救了起來。

             A pregnant lady rescued him from the Suzhou River.           

             這是從這裡去蘇州的直達列車嗎?

             Be this a through carriage for Suzhou from here?

3. JAX-RS

3.1The common interface

Jar name RsClient.jar
Interface

package com.cn;

import java.util.Map;

public interface RsClient {

      public <K, V> String getResult(String applyKey, String httpType, String type, Map<K,V>  parameters, String... message);

}

Parameters
Type Name Description
String applyKey User's Key, user's verification
String httpType Http calling type, GET/POST/PUT/DELETE
String type The provided Web Service type name
Map<K,V> parameters httpType is POST, the related parameters
String… message The input parameters
Return
Type Description
String The RESTful Web Service calling result

3.2The functions

3.2.1 Validations

3.2.1.2 CheckapplyKey

ToDo

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS
3.2.1.2 Checktype

Check resource/resource.properties which key = support.type to verify if theincoming type is support or not.

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS
3.2.1.3 Check HTTPtype

Only support GET/POST/PUT/DELETE now.

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

3.2.2 Prepare RESTful WebService URL

Use the configured method and path to getthe correct URL.

For example

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

3.2.3 Get RESTful Web ServiceResponse

To call the related URL, get the response.

For example

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

3.2.4 Parse the response

Use the configured result key word to parseand format the result.

For example

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

3.3 Tocall common JAX-RS

3.3.1 Validations

       Pass.

3.3.2 Prepare RESTful WebService URL

Use the configured method and path to getthe correct URL.

For example

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

1. Get the base context path http://www.weather.com.cn/data/sk/

2. Use the method to get the pathInfogetWeatherPathInfo

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

Get city code 101190401 for蘇州

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

3. If no "weather.path.info.method",use "weather.path.info.url" directly;

4.Get pathInfo suffix.

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

5. Get the final URL: http://www.weather.com.cn/data/sk/101190401.html

3.3.3 Get RESTful Web ServiceResponse

To call the related URL, get the response.

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

For example

{

    "weatherinfo":{

        "city":"蘇州",

       "cityid":"101190401",

        "temp":"17",

        "WD":"東南風",

        "WS":"2級",

        "SD":"44%",

        "WSE":"2",

        "time":"17:05",

        "isRadar":"0",

        "Radar":"",

        "njd":"暫無實況",

        "qy":"1017",

        "rain":"0"

    }

}

3.3.4 Parse the response

城市:蘇州

溫度:17

風向:東南風

風速:2級

濕度:44%

釋出時間:17:05

3.4 Tocall private JAX-RS which released by ourself

         TODO.

3.5 Othercommon JAX-RS

3.5.1 PM 2.5

        RsClientImplrsClient =new RsClientImpl();

         rsClient.getResult("","GET","pm2.5",null,"蘇州");

                   ==>

3.5.2 O3

        RsClientImplrsClient =new RsClientImpl();

         rsClient.getResult("","GET","o3",null,"蘇州");

                   ==>

WebService與RESTful WebService1. Summary2. JAX-WS3. JAX-RS

 代碼連結

繼續閱讀