亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Idea調用WebService的關鍵步驟和注意事項

 更新時間:2025年01月17日 08:35:36   作者:A.不愿起名字的人  
這篇文章主要介紹了如何在Idea中調用WebService,包括理解WebService的基本概念、獲取WSDL文件、閱讀和理解WSDL文件、選擇對接測試工具或方式、發(fā)送請求和接收響應、處理響應結果以及錯誤處理,需要的朋友可以參考下

前言

? WebService是一種基于網(wǎng)絡的技術,它允許不同的應用程序在互聯(lián)網(wǎng)上相互通信。要進行WebService對接,以下是一些關鍵步驟和注意事項:

一、理解WebService的基本概念

  • 定義:WebService是一種基于標準化協(xié)議和格式的應用程序接口(API),它使用XML和HTTP來進行通信。
  • 特點:可以跨平臺、跨語言進行數(shù)據(jù)傳輸和應用程序集成。

二、獲取WSDL文件

  • WSDL定義:WSDL是Web服務描述語言(Web Services Description Language)的縮寫,是一種基于XML的語言,用于描述Web服務的接口、方法和數(shù)據(jù)類型。
  • 獲取方式:通常,可以通過訪問WebService的WSDL地址來獲取WSDL文件。WSDL地址通常以“.wsdl”結尾,例如:http://localhost:8082/web/services/weather?wsdl。
  • Idea的操作過程,

    首先建立一個webService文件夾在項目中

    選擇此文件夾,點擊Tools中的XML webService,選擇Generate

    輸入地址點擊確定就行

    會生成多個類和方法名就直接可以調用

三、閱讀和理解WSDL文件

  • 服務定義:WSDL文件包含一個或多個服務定義,每個服務定義描述了一個或多個相關的操作,以及它們接受和返回的消息的格式。
  • 命名空間:WSDL中的命名空間用于唯一標識WSDL中包含的類型、元素和消息。
  • 操作和方法:WSDL文件還描述了Web服務的地址、協(xié)議和傳輸機制等信息,以及具體的操作和方法。

四、選擇對接測試工具或方式

  • SOAP UI:一個流行的WebService測試工具,可以用于查看WebService的接口信息、發(fā)送請求和接收響應。
  • Postman:另一個常用的API測試工具,同樣可以用于WebService的對接測試。
  • 編程方式:在代碼中,可以通過發(fā)送HTTP請求來調用WebService,請求參數(shù)需要按照WSDL文件中定義的格式封裝成XML格式。

五、發(fā)送請求和接收響應

  • 請求格式:根據(jù)WSDL文件中的定義,構造符合要求的SOAP請求報文。請求報文通常包含Envelope(信封)、Header(頭部)和Body(正文)等部分。
  • 發(fā)送請求:使用選擇的對接工具或方式發(fā)送請求到WebService的服務端點。
  • 接收響應:服務端點接收到請求后,會進行解析并調用相應的Web服務方法,然后將結果封裝成HTTP響應返回。響應報文同樣遵循SOAP協(xié)議,并包含Envelope、Header和Body等部分。

六、直接通過xml和http請求訪問

? 1. 引用包

        <!--
             使用apache的httpclient發(fā)送http,需要引入httpclient依賴;
             使用OMElement需要引入axis2-transport-http依賴;改以來本身帶有httpclient依賴,所以
                 我們不在需要單獨引入httpclient依賴了
         -->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-http</artifactId>
            <version>1.7.8</version>
        </dependency>

? 2.

package com.smart.util;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMXMLBuilderFactory;
import org.apache.http.client.ClientProtocolException;
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.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class AbcAxis2DemoApplicationUtils {
	public static String doPost(String partNo) throws  IOException {
		// webservice的wsdl地址
		final String wsdlURL = ""
		// 設置編碼。(因為是直接傳的xml,所以我們設置為text/xml;charset=utf8)
		final String contentType = "text/xml;charset=utf8";
		/// 拼接要傳遞的xml數(shù)據(jù)(注意:此xml數(shù)據(jù)的模板我們根據(jù)wsdlURL從SoapUI中獲得,只需要修改對應的變量值即可)
		StringBuffer xMLcontent = new StringBuffer("");
		xMLcontent.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:DefaultNamespace\">\n");
		xMLcontent.append("   <soapenv:Header/>\n");
		xMLcontent.append("   <soapenv:Body>\n");
		xMLcontent.append("     <urn:字段1>Shanghai</urn:字段1>\n");
		xMLcontent.append("     <urn:字段2>" + "TAN2921" + "</urn:字段2>\n");
		xMLcontent.append("   </soapenv:Body>\n");
		xMLcontent.append("</soapenv:Envelope>");

		// 調用工具類方法發(fā)送http請求
		String responseXML = HttpSendUtil.doHttpPostByHttpClient(wsdlURL, contentType, xMLcontent.toString());
		// 當然我們也可以調用這個工具類方法發(fā)送http請求
		// String responseXML = HttpSendUtil.doHttpPostByRestTemplate(wsdlURL, contentType, xMLcontent.toString());

		// 利用axis2的OMElement,將xml數(shù)據(jù)轉換為OMElement
		OMElement omElement = OMXMLBuilderFactory
				.createOMBuilder(new ByteArrayInputStream(responseXML.getBytes()), "utf-8").getDocumentElement();
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setNamespaceAware(true); // 設置為true以處理命名空間
			DocumentBuilder builder = factory.newDocumentBuilder();
			InputSource inputSource = new InputSource(new StringReader(responseXML));
			Document document = builder.parse(inputSource);

			// 獲取所有名為"OUTPUT_SHIPTOPARTYReturn"的元素,并考慮命名空間
			NodeList nodeList = document.getElementsByTagNameNS("urn:DefaultNamespace", "OUTPUT_SHIPTOPARTYReturn");

			for (int i = 0; i < nodeList.getLength(); i++) {
				Element element = (Element) nodeList.item(i);
				// 檢查元素是否有文本內容(即非空且非自閉合標簽)
				if (element.hasChildNodes() && element.getFirstChild().getNodeType() == Node.TEXT_NODE) {
					String textContent = element.getTextContent();
					System.out.println("Extracted value: " + textContent);
					return textContent;
				}
			}

		} catch (ParserConfigurationException | SAXException | IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	public static String getNodeValue(Document document, String nodePaht) {
		XPathFactory xpfactory = XPathFactory.newInstance();
		XPath path = xpfactory.newXPath();
		String servInitrBrch = "";
		try {
			servInitrBrch = path.evaluate(nodePaht, document);
		} catch (XPathExpressionException e) {
			e.printStackTrace();
		}
		return servInitrBrch;
	}



	public static Document StringTOXml(String str) {

		StringBuilder sXML = new StringBuilder();
		sXML.append(str);
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		Document doc = null;
		try {
			InputStream is = new ByteArrayInputStream(sXML.toString().getBytes("utf-8"));
			doc = dbf.newDocumentBuilder().parse(is);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return doc;
	}
}
/**
 * HTTP工具類
 *
 * @author JustryDeng
 * @DATE 2018年9月22日 下午10:29:08
 */
class HttpSendUtil {

	/**
	 * 使用apache的HttpClient發(fā)送http
	 *
	 * @param wsdlURL
	 *            請求URL
	 * @param contentType
	 *            如:application/json;charset=utf8
	 * @param content
	 *            數(shù)據(jù)內容
	 * @DATE 2018年9月22日 下午10:29:17
	 */
	static String doHttpPostByHttpClient(final String wsdlURL, final String contentType, final String content)
			throws ClientProtocolException, IOException {
		// 獲得Http客戶端(可以理解為:你得先有一個瀏覽器;注意:實際上HttpClient與瀏覽器是不一樣的)
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		// 創(chuàng)建Post請求
		HttpPost httpPost = new HttpPost(wsdlURL);
		StringEntity entity = new StringEntity(content.toString(), "UTF-8");
		// 將數(shù)據(jù)放入entity中
		httpPost.setEntity(entity);
		httpPost.setHeader("Content-Type", contentType);
		// 響應模型
		CloseableHttpResponse response = null;
		String result = null;
		try {
			// 由客戶端執(zhí)行(發(fā)送)Post請求
			response = httpClient.execute(httpPost);
			// 從響應模型中獲取響應實體
			// 注意:和doHttpPostByRestTemplate方法用的不是同一個HttpEntity
			org.apache.http.HttpEntity responseEntity = response.getEntity();
			System.out.println("響應ContentType為:" + responseEntity.getContentType());
			System.out.println("響應狀態(tài)為:" + response.getStatusLine());
			if (responseEntity != null) {
				result = EntityUtils.toString(responseEntity);
				System.out.println("響應內容為:" + result);
			}
		} finally {
			// 釋放資源
			if (httpClient != null) {
				httpClient.close();
			}
			if (response != null) {
				response.close();
			}
		}
		return result;
	}

	/**
	 * 使用springframework的RestTemplate發(fā)送http
	 *
	 * @param wsdlURL
	 *            請求URL
	 * @param contentType
	 *            如:application/json;charset=utf8
	 * @param content
	 *            數(shù)據(jù)內容
	 * @DATE 2018年9月22日 下午10:30:48
	 */
	static String doHttpPostByRestTemplate(final String wsdlURL, final String contentType, final String content) {
		// http使用無參構造;https需要使用有參構造
		RestTemplate restTemplate = new RestTemplate();
		// 解決中文亂碼
		List<HttpMessageConverter<?>> converterList = restTemplate.getMessageConverters();
		converterList.remove(1);
		HttpMessageConverter<?> converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
		converterList.add(1, converter);
		restTemplate.setMessageConverters(converterList);
		// 設置Content-Type
		HttpHeaders headers = new HttpHeaders();
		headers.remove("Content-Type");
		headers.add("Content-Type", contentType);
		// 數(shù)據(jù)信息封裝
		// 注意:和doHttpPostByHttpClient方法用的不是同一個HttpEntity
		org.springframework.http.HttpEntity<String> formEntity = new org.springframework.http.HttpEntity<String>(
				content, headers);
		String result = restTemplate.postForObject(wsdlURL, formEntity, String.class);
		return result;
	}
}

七、處理響應結果

  • 解析響應:將接收到的響應報文進行解析,提取出需要的數(shù)據(jù)。這通常涉及到對XML格式的響應報文進行解析和處理。
  • 錯誤處理:在對接過程中,可能會遇到各種錯誤和異常情況。因此,需要添加適當?shù)腻e誤處理邏輯來應對這些情況。

總結 

到此這篇關于Idea調用WebService的關鍵步驟和注意事項的文章就介紹到這了,更多相關Idea調用WebService內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 利用Java反射機制實現(xiàn)對象相同字段的復制操作

    利用Java反射機制實現(xiàn)對象相同字段的復制操作

    這篇文章主要介紹了利用Java反射機制實現(xiàn)對象相同字段的復制操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Maven項目讀取resources文件路徑問題解決方案

    Maven項目讀取resources文件路徑問題解決方案

    這篇文章主要介紹了Maven項目讀取resources文件路徑問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot項目如何打war包問題詳解

    SpringBoot項目如何打war包問題詳解

    傳統(tǒng)的部署方式:將項目打成war包,放入tomcat的webapps目錄下面,啟動tomcat,即可訪問.文中有非常詳細的介紹,對正在學習springboot的小伙伴很有幫助,需要的朋友可以參考下
    2021-05-05
  • java實現(xiàn)操作系統(tǒng)中的最佳置換Optimal算法

    java實現(xiàn)操作系統(tǒng)中的最佳置換Optimal算法

    這篇文章主要介紹了java實現(xiàn)操作系統(tǒng)中的最佳置換Optimal算法 ,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • SpringBoot中的@Component注解源碼

    SpringBoot中的@Component注解源碼

    在SpringBoot中,@Component注解用于標識一個類為Bean,并由Spring進行管理,它需要配合@ComponentScan注解使用,后者負責掃描包含@Component的類,并進行自動裝配,本文給大家介紹SpringBoot中的@Component注解源碼,感興趣的朋友一起看看吧
    2024-01-01
  • java并發(fā)編程synchronized底層實現(xiàn)原理

    java并發(fā)編程synchronized底層實現(xiàn)原理

    這篇文章主要介紹了java并發(fā)編程synchronized底層實現(xiàn)原理
    2022-02-02
  • Kotlin基礎教程之Run,標簽Label,函數(shù)Function-Type

    Kotlin基礎教程之Run,標簽Label,函數(shù)Function-Type

    這篇文章主要介紹了Kotlin基礎教程之Run,標簽Label,函數(shù)Function-Type的相關資料,需要的朋友可以參考下
    2017-05-05
  • List集合對象中按照不同屬性大小排序的實例

    List集合對象中按照不同屬性大小排序的實例

    下面小編就為大家?guī)硪黄狶ist集合對象中按照不同屬性大小排序的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Springboot Redis?哨兵模式的實現(xiàn)示例

    Springboot Redis?哨兵模式的實現(xiàn)示例

    本文主要介紹了Springboot Redis?哨兵模式的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • MyBatis中的N+1問題的解決方法

    MyBatis中的N+1問題的解決方法

    本文主要介紹了MyBatis中的N+1問題的四種解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-12-12

最新評論