将 XML 作为参数传递给 Web 服务

编程入门 行业动态 更新时间:2024-10-25 04:24:47
本文介绍了将 XML 作为参数传递给 Web 服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在回答另一个问题中有人提到将 XML 作为字符串参数传递给 Web 服务是不好的做法.这是什么原因?

In an answer to another question it was mentioned that passing XML as a string parameter to a web service was bad practice. What is the reason for this?

推荐答案

我使用过许多将简单 XML 值作为参数或将它们作为输出返回的 Web 服务,所以我不确定为什么有人会认为它是一个不好的做法,但我可以描述一些弱点.

I have used many web services that take simple XML values as a parameter or return them as output, so I am not sure exactly why someone would consider it to be a bad practice, but I could describe a few of the weaknesses.

我认为使用任意 XML 作为输入参数的主要缺点是它本身不提供强类型.如果您使用基于 SOAP 的 Web 服务和 WSDL 概述 Web 服务输入和输出变量,那么使用基本 XML 作为变量不会为用户提供太多信息.如果您有一个基本的字符串输入值,您假设用户将向其输入 XML 数据,则尤其如此.更好的方法是使用 XMLElement 或 XMLNode 类型而不是标准字符串,这样至少可以对 Web 服务中的有效 XML 进行基本级别的类型检查.SOAP 和 WSDL 的想法是创建强类型参数,以便可以在应用程序之间的线路上来回传递完整的对象.您基本上可以创建一个对象并将其用作输入或输出值的基础,SOAP 将自动为您处理序列化和反序列化模式的创建.

The main downside that I can see to using arbitrary XML as an input parameter is that by itself it does not provide strong typing. If you are using a SOAP based web services with a WSDL that outlines the web service input and output variables then using basic XML as a variable does not give the user very much information. This is especially the case if you have a basic string input value to which you assume the user will enter XML data. A better approach is to use an XMLElement or XMLNode type instead of a standard string, so that there is at least a basic level of type checking for valid XML in the web service. The idea with SOAP and WSDL though are to create strongly typed parameters so that full objects can be passed back and forth across the wire between applications. You basically can create an object and use that as the basis for your input or output values and SOAP will handle the creation of a schema for serialization and de-serialization for you automatically.

问题在于,使用复杂的数据类型会显着增加调用 Web 服务所需的复杂性.Web 服务 提供多种风格,从简单的REST 服务到 WS - * 强类型消息的协议.使用 Plain Old XML 没有架构肯定是一个坏主意,如果你正在建立一个企业基于 WS - * 的业务 Web 服务,但如果您正在创建一个简单的 REST 服务,那么 POX 可能会很好地满足您的需求.

The problem is that using complex data types can significantly increase the complexity required for calling the web service. Web services are offered in a variety of flavors from simple REST services to a complex web of WS - * protocols for strongly typed messages. Using Plain Old XML with no schema would definitely be a bad idea if you are building a Business to Business web service based on WS - *, but if you are creating a simple REST service then POX might suit your needs just fine.

这篇关于将 XML 作为参数传递给 Web 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 06:37:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1130484.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   XML   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!