WebService學(xué)習(xí)整理及http、cxf使用方式

schema約束文件說明

image.jpeg

一、什么是WebService?

基于web的服務(wù),它是一種跨語言、跨平臺的規(guī)范。實際是多個跨平臺、跨語言通信方案的整合

可以跨語言、遠(yuǎn)程調(diào)用、跨平臺調(diào)用;基于xml格式文檔通過其他具體的協(xié)議進(jìn)行服務(wù)與服務(wù)之間的通信。

在java1.6版本之后開始支持

二、webService中的專業(yè)術(shù)語

WSDL:WebService Description Language 即web服務(wù)的描述語言,實際就是定義webservice服務(wù)并發(fā)布之后的xml格式的文檔

1、說明服務(wù)在什么地方,也就是標(biāo)明服務(wù)的地址

2、說明服務(wù)提供了那些方法,該如何調(diào)用

SOAP:Simple Object Access Protocol 即簡單對象訪問協(xié)議??梢岳斫鉃樵贖ttp基礎(chǔ)上+xml格式數(shù)據(jù);主要包含三個部分

1、Envelope 必須的部分,以xml的根元素出現(xiàn)

2、Headers 可選的

3、Body 必須的部分,在這一部分,包含要執(zhí)行的服務(wù)器方法和發(fā)送給服務(wù)器的參數(shù)

SEI: WebService Endppint Interface webService的終端接口,就是服務(wù)器端用來處理請求的接口

CXF:Celtix+Xfire 。 一個apache的用于開發(fā)webService服務(wù)端和客戶端的框架

webService文檔結(jié)構(gòu)(wsdl文檔結(jié)構(gòu))

image.jpeg

該xml文檔的格式由上至下定義了一系列關(guān)于發(fā)布的接口的信息,引用關(guān)系是由下往上進(jìn)行引用的

三、webService開發(fā)

1、使用jdk原生注解方式開發(fā)

mac使用wsimport命令生成客戶端代碼時如果報錯 。 zsh: no matches found: http://127.0.0.1:8080/webservice/sayhello?wsdl

解決方案 在mac系統(tǒng) 文件 。 ~/.zshrc 文件中 加入如下代碼 setopt no_nomatch 保存之后重新加載該文件 source ~/.zshrc 。 注意:默認(rèn)系統(tǒng)中可能沒有該文件 。 直接新建文件即可

在類上使用@WebService注解表明該類是用于發(fā)布webService服務(wù)的類或者接口

在要發(fā)布的方法上使用@WebMethod注解。針對命名空間及其他參數(shù)可以使用注解中的屬性進(jìn)行指定

2、cxf方式

cxf支持的數(shù)據(jù)類型:相對于jdk的方式 。 它支持map數(shù)據(jù)結(jié)構(gòu),支持的數(shù)據(jù)結(jié)構(gòu)更加豐富

CXF的攔截器:主要作用就是動態(tài)的操作請求和響應(yīng)的數(shù)據(jù)

服務(wù)端開發(fā)和jdk原生方式類似。

客戶端可以采用cxf的wsdl2Java命令生成客戶端接口(該方式需要在本地安裝cxf),也可以使用csf代理工廠生產(chǎn)客戶端接口進(jìn)行實現(xiàn)

、、、

package cn.lyn.webservice.client;

import cn.lyn.webservice.interceptor.CxfInterceptor;
import cn.lyn.webservice.serviceone.WebServiceInterface;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.message.Message;

import java.util.List;

/**

  • @author LengYouNuan
  • @create 2021-05-24 下午2:13
    */

public class CxfClient {

public static void main(String[] args) {
    JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();

    jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:8080/soap/sayHello");

    jaxWsProxyFactoryBean.setServiceClass(WebServiceInterface.class);


    //客戶端日志出攔截器
    List<Interceptor<? extends Message>> outFaultInterceptors = jaxWsProxyFactoryBean.getOutFaultInterceptors();
    outFaultInterceptors.add(new CxfInterceptor());

    //客戶端日志入攔截器
    List<Interceptor<? extends Message>> inFaultInterceptors = jaxWsProxyFactoryBean.getInFaultInterceptors();
    inFaultInterceptors.add(new CxfInterceptor());


    WebServiceInterface webServiceInterface = (WebServiceInterface) jaxWsProxyFactoryBean.create();
    String aha = webServiceInterface.sayHello("aha");
    System.out.println(aha);
}

}
、、、

3、http方式

該方式是應(yīng)用在客戶端方的。采用http請求的方式封裝相關(guān)的參數(shù)進(jìn)行接口的訪問。需要注意的是SOAP參數(shù)的拼接??梢圆捎肧OAPUI客戶端工具根據(jù)wsdl文檔生成請求報文,也可以自己根據(jù)wsdl文檔自行拼接soap請求報文信息。

得到的響應(yīng)數(shù)據(jù)一般都是xml格式,一般需要有效響應(yīng)數(shù)據(jù)都在<return></return>標(biāo)簽里,需要自行解析
、、、

package cn.lyn.webservice.client;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.nio.charset.Charset;

/**

  • http方式調(diào)用webservice接口

  • @author LengYouNuan

  • @create 2021-05-24 下午4:30
    */
    public class HttpClientMethod {

    private static final Logger logger = LogManager.getLogger(HttpClientMethod.class);

    public static void main(String[] args) {

     String  name="aaa";
    
     //soap 參數(shù)
     String s = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ws='http://自己的接口服務(wù)地址/'>\n" +
             "    <soapenv:Header/>\n" +
             "    <soapenv:Body>\n" +
             "        <ws:sayHello>\n" +
             "            <arg0>" + name + "</arg0>\n" +
             "        </ws:sayHello>\n" +
             "    </soapenv:Body>\n" +
             "</soapenv:Envelope>";
     String soapXml= s;
    
    
     //獲取http構(gòu)建器對象
     HttpClientBuilder builder = HttpClientBuilder.create();
     CloseableHttpClient httpClient = builder.build();
    
     //封裝httppost請求對象
     HttpPost httpPost = new HttpPost("http://127.0.0.1:8080/soap/sayHello");
     httpPost.setConfig(RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build());
     try {
         httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");
         httpPost.setHeader("SOAPAction", "");
         StringEntity data = new StringEntity(soapXml,
                 Charset.forName("UTF-8"));
         httpPost.setEntity(data);  //設(shè)置post請求參數(shù)實體
    
         //執(zhí)行http請求
         CloseableHttpResponse response = httpClient
                 .execute(httpPost);
         HttpEntity httpEntity = response.getEntity();
         if (httpEntity != null) {
             // 打印響應(yīng)內(nèi)容
             String retStr = EntityUtils.toString(httpEntity, "UTF-8");
             logger.info("==================================================================================");
             logger.info("==================================================================================");
             logger.info("response:=========》" + retStr);
         }
         // 釋放資源
         httpClient.close();
     } catch (Exception e) {
         logger.error("exception in doPostSoap1_1", e);
     }
    

    }
    }
    、、、

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容