Idea调用WebService的关键步骤和注意事项

2025-01-17 16:50

本文主要是介绍Idea调用WebService的关键步骤和注意事项,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《Idea调用WebService的关键步骤和注意事项》:本文主要介绍如何在Idea中调用WebService,包括理解WebService的基本概念、获取WSDL文件、阅读和理解WSDL文件、选...

前言

​ WebService是一种基于网络的技术,它允许不同的应用程序在互联网上相互通信。要进行WebService对接,以下是一些关键步骤和注意事项:

一、理解WebService的基本概念

  • 定义:WebService是一种基于标准化协议和格式的应用程序接口(API),它使用XML和HTTP来进行通信。
  • 特点:可以跨平台、跨语言进行数据传输和应用程序集成。

二、获取WSDL文件

  • WSDL定义:WSDL是Web服务描述语言(Web Services Description Language)的缩写,是一种基于XML的语言,用于描述Web服务的接口、方法和数据类型。
  • 获取方式:通常,可以通过访问WebService的WSDL地址来获取WSDL文件。WSDL地址通常以“.wsdl”结尾,例如:http://localhost:8082/web/services/weather?wsdl。
  • Idea的操作过程,

    首先建立一个webService文件夹在项目中

    Idea调用WebService的关键步骤和注意事项

    选择此文件夹,点击Tools中的XML webService,选择Generate

    Idea调用WebService的关键步骤和注意事项

    输入地址点击确定就行

    Idea调用WebService的关键步骤和注意事项

    会生成多个类和方法名就直接可以调用

三、阅读和理解WSDL文件

  • 服务定义:WSDL文件包含一个或多个服务定义,每个服务定义描述了一个或多个相关的操作,以及它们接受和返回的消息的格式。
  • 命名空间:WSDL中的命名空间用于唯一标识WSDL中包含的类型、元素和消息。
  • 操作和方法:WSDL文件还描述了Web服务的地址、协议和传输机制等信息,以及具体的操作和方法。

四、选择对接测试工具或方式

  • SOAP UI:一个流行的WebService测试工具,可以用于查看WebService的接口信息、发送请求和接收响应。
  • Postman:另一个常用的API测试工具,同样可以用于WebService的对接测试。
  • 编程方式:在代码中,可以通过发送HTTP请求来调用WebService,请求参数需要按照WSDL文件中定义的格式封装成XML格式。

五、发送请求和接收响应

  • 请求格式:根据WSDL文件中的定义,构造符合要求的SOAP请求报文。请求报文通常包含Envelope(信封)、Header(头部)和Body(正文)等部分。
  • 发送请求:使用选择的对接工具或方式发送请求到WebService的服务端点。
  • 接收响应:服务端点接收到请求后,会进行解析并调用相应的Web服务方法,然后将结果封装成HTTP响应返回。响应报文同样遵循SOAP协议,并包含Envelope、Header和Body等部分。

六、直接通过xml和http请求访问

​ 1. 引用包

        <!--
             使用apache的httpclient发送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&jsgt;

​ 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数据(注意:此xml数据的模板我们根据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>");

		// 调用工具类方法发送http请求
		String responseXML = HttpSendUtil.doHttpPostByHttpClient(wsdlURL, contentType, xMLcontent.toString());
		// 当然我们也可以调用这个工具类方法发送http请求
		// String responseXML = HttpSendUtil.doHttpPostByRestTemplate(wsdlURL, contentType, xMLcontent.toString());

		// 利用axis2的OMElement,将xml数据转换为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:DefaultandroidNamespace", "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: " + textContenhttp://www.chinasem.cnt);
					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发送http
	 *
	 * @param wsdlURL
	 *            请求URL
	 * @param contentType
	 *            如:application/json;charset=utf8
	 * @param content
	 *            数据内容
	 * @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();
		// 创建Post请求
		HttpPost httpPost = new HttpPost(wsdlURL);
	js	StringEntity entity = new StringEntity(content.toString(), "UTF-8");
		// 将数据放入entity中
		httpPost.setEntity(entity);
		httpPost.setHeader("Content-Type", contentType);
		// 响应模型
		CloseableHttpResponse response = null;
		String result = null;
		try {
			// 由客户端执行(发送)Post请求
			response = httpClient.execute(httpPost);
			// 从响应模型中获取响应实体
			// 注意:和doHttpPostByRestTemplate方法用的不是同一个HttpEntity
			org.apache.http.HttpEntity responseEntity = response.getEntity();
			System.out.println("响应ContentType为:" + responseEntity.getContentType());
			System.out.println("响应状态为:" + 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发送http
	 *
	 * @param wsdlURL
	 *            请求URL
	 * @param contentType
	 *            如:application/json;charset=utf8
	 * @param content
	 *            数据内容
	 * @DATE 2018年9月22日 下午10:30:4编程8
	 */
	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);
		// 数据信息封装
		// 注意:和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;
	}
}

七、处理响应结果

  • 解析响应:将接收到的响应报文进行解析,提取出需要的数据。这通常涉及到对XML格式的响应报文进行解析和处理。
  • 错误处理:在对接过程中,可能会遇到各种错误和异常情况。因此,需要添加适当的错误处理逻辑来应对这些情况。

总结 

到此这篇关于Idea调用WebService的关键步骤和注意事项的文章就介绍到这了,更多相关Idea调用WebService内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持China编程(www.chinasem.cn)!

这篇关于Idea调用WebService的关键步骤和注意事项的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1153110

相关文章

IDEA和GIT关于文件中LF和CRLF问题及解决

《IDEA和GIT关于文件中LF和CRLF问题及解决》文章总结:因IDEA默认使用CRLF换行符导致Shell脚本在Linux运行报错,需在编辑器和Git中统一为LF,通过调整Git的core.aut... 目录问题描述问题思考解决过程总结问题描述项目软件安装shell脚本上git仓库管理,但拉取后,上l

idea npm install很慢问题及解决(nodejs)

《ideanpminstall很慢问题及解决(nodejs)》npm安装速度慢可通过配置国内镜像源(如淘宝)、清理缓存及切换工具解决,建议设置全局镜像(npmconfigsetregistryht... 目录idea npm install很慢(nodejs)配置国内镜像源清理缓存总结idea npm in

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat

idea突然报错Malformed \uxxxx encoding问题及解决

《idea突然报错Malformeduxxxxencoding问题及解决》Maven项目在切换Git分支时报错,提示project元素为描述符根元素,解决方法:删除Maven仓库中的resolv... 目www.chinasem.cn录问题解决方式总结问题idea 上的 maven China编程项目突然报错,是

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

SpringBoot集成WebService(wsdl)实践

《SpringBoot集成WebService(wsdl)实践》文章介绍了SpringBoot项目中通过缓存IWebService接口实现类的泛型入参类型,减少反射调用提升性能的实现方案,包含依赖配置... 目录pom.XML创建入口ApplicationContextUtils.JavaJacksonUt

如何正确识别一台POE交换机的好坏? 选购可靠的POE交换机注意事项

《如何正确识别一台POE交换机的好坏?选购可靠的POE交换机注意事项》POE技术已经历多年发展,广泛应用于安防监控和无线覆盖等领域,需求量大,但质量参差不齐,市场上POE交换机的品牌繁多,如何正确识... 目录生产标识1. 必须包含的信息2. 劣质设备的常见问题供电标准1. 正规的 POE 标准2. 劣质设

使用Go调用第三方API的方法详解

《使用Go调用第三方API的方法详解》在现代应用开发中,调用第三方API是非常常见的场景,比如获取天气预报、翻译文本、发送短信等,Go作为一门高效并发的编程语言,拥有强大的标准库和丰富的第三方库,可以... 目录引言一、准备工作二、案例1:调用天气查询 API1. 注册并获取 API Key2. 代码实现3

Java Stream 并行流简介、使用与注意事项小结

《JavaStream并行流简介、使用与注意事项小结》Java8并行流基于StreamAPI,利用多核CPU提升计算密集型任务效率,但需注意线程安全、顺序不确定及线程池管理,可通过自定义线程池与C... 目录1. 并行流简介​特点:​2. 并行流的简单使用​示例:并行流的基本使用​3. 配合自定义线程池​示

Mac电脑如何通过 IntelliJ IDEA 远程连接 MySQL

《Mac电脑如何通过IntelliJIDEA远程连接MySQL》本文详解Mac通过IntelliJIDEA远程连接MySQL的步骤,本文通过图文并茂的形式给大家介绍的非常详细,感兴趣的朋友跟... 目录MAC电脑通过 IntelliJ IDEA 远程连接 mysql 的详细教程一、前缀条件确认二、打开 ID