一个网络服务中是否可以有多个 xsd 映射? [英] Is it possible to have multiple xsd mappings in a webservice?

查看:41
本文介绍了一个网络服务中是否可以有多个 xsd 映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是网络服务的新手,我正面临一个设计问题,我们想要创建一个将接收 2 个参数的网络服务(最好是基于 SOAP 的):

  • 一个字符串变量(客户端名称);
  • XML 数据.

此网络服务目前有 2 个不同的使用者,但它们的数量会增加.所有消费者都有他们将要发送的单独的 XSD 数据,例如:

消费者 1:

<客户><FirstName>FirName</FirstName><LastName>LasName</LastName></客户><账户><号码>112971</号码></帐户><订单><OrderNum>0092123</OrderNum></订单><ConData>

消费者 2:

<系统数据><CustomerFirstName>姓名</CustomerFirstName><CustomerLastName>姓名</CustomerLastName><AccountNumber>Au1o2n</AccountNumber><OrderNum>koo912</OrderNum></系统数据><ConData>

该 Web 服务应该从这些客户端获取 XML 数据,并根据消费者名称将其存储到数据库中.我们不需要任何处理来验证 XML,我们只需将其存储在数据库中即可.

不过,作为一个好的设计,我想用显式参数定义我的网络服务.问题是,由于它们的 XML 数据格式不同,我无法在我的 WSDL 中使用单个 XSD 模式作为参数.我可以想到 2 个选项来解决这个问题:

1.让他们在我的 Web 服务中将 XML 数据作为字符串而不是 XSD 映射参数传递. 虽然这是一种有效的方法,但根据我目前在论坛上阅读的内容,这似乎是一个糟糕的设计,因为它使验证无效并且需要大量的手动编组和解组等问题.此外,我的 WSDL 将无法向我的客户定义预期的格式等.

2.创建 2 个独立的 Web 服务,这些服务将使用自己的 XSD 处理不同的个人消费者.这再次似乎不太可行,因为将来如果我们添加更多消费者,我们将不得不为相同的操作添加具有重复代码的新网络服务等.

我想看看是否有一种方法可以在单个 Web 服务 WSDL 中定义多个 XSD,或者是否有更好的方法来解决这个问题.

顺便说一句,我更喜欢使用基于 SOAP 的 Web 服务,但如果使用 RESTful Web 服务以更好的方式解决此问题,那么我也可以考虑.

解决方案

您不需要为每个客户端拥有不同的 Web 服务或框架,您只需要不同的 WSDL..>

忘记将这个 XML 参数作为字符串发送,它有一些缺点,我不会在这里重复,因为您也已经阅读过它.所以你需要将它作为 XML 发送.

要将其保留在一个 Web 服务中,您需要创建 Web 服务,以便它接受任何 XML 作为输入(输入 作为参数).类型 <xsd:any> 将意味着任何类型的 XML,但对于初学者来说,至少它是可以验证结构的 XML.

这简化了 Web 服务的版本控制,因为在添加另一种 XML 类型的输入时您不需要更改代码,但不幸的是,您丢失了 Web 服务的强类型,并且为 Web 服务生成的 WSDL 不会说明任何有关它所期望的 XML 的结构(任何 XML 都太模糊了).您的 Web 服务框架将使用类似 ObjectXmlElementXmlDocument 或任何代码中的类型创建.您所有的客户端存根也会发生同样的情况.

但是对于您的客户端,这可以通过为每个客户端提供不同的 WSDL 来从 WSDL 中解决.

为此,您为所有客户端(<types> 部分除外)创建具有通用内容的 Web 服务的 WSDL,然后将每个客户端的通用 WSDL 与它们自己的 XML 模式合并.然后,您将为每个客户端在不同的 URL 上公开每个 WSDL.

现在客户端将能够基于他们自己特定的 WSDL 生成强类型存根.

当客户端调用您的 Web 服务时,您会识别客户端 XML 架构并使用它来验证 XML 参数并查看它是否是预期的内容.

如果上述内容看起来很多,那么您始终可以选择松散类型的 RESTful Web 服务.无论您选择哪种解决方案,只要确保您使用发送它的客户端的架构来验证传入的 XML.

I'm new to web services and I'm facing a design issue where we want to create a web service (preferably SOAP based) which will receive 2 parameters:

  • a string variable (Client Name);
  • XML data.

There are currently 2 different consumers of this web service but their number will increase. All consumers have separate XSD data that they will be sending, for example:

Consumer1:

<ConData>
    <Customer>
        <FirstName>FirName</FirstName>
        <LastName>LasName</LastName>
    </Customer>
    <Acocunt>
        <Number>112971</Number>
    </Account>
    <Order>
        <OrderNum>0092123</OrderNum>
    </Order>
<ConData>

Consumer2:

<ConData>
    <SysData>
        <CustomerFirstName>Name</CustomerFirstName>
        <CustomerLastName>Name</CustomerLastName>
        <AccountNumber>Au1o2n</AccountNumber>
        <OrderNum>koo912</OrderNum>
    </SysData>
<ConData>

This web service is supposed to take the XML data from these clients and store it into the database based on the consumer name. There is no processing required on our end for validating the XML as such, we just take it and store it in the database.

As a good design though, I'd like to define my web service with explicit parameters. The problem is that since their XML data format is different I'm not able to use a single XSD schema as a parameter in my WSDL. I can think of 2 options to address this:

1. Let them pass XML data as String in my web service instead of XSD mapping parameter. Though this is a valid approach, it seems like a bad design based on what I've read so far on the forums as it voids validation and needs lots of manual marshaling and unmarshaling and other problems. Plus my WSDL will not be able to define to my clients what format is expected etc.

2. Create 2 separate web services which will handle different individual consumers with their own XSD's. This again seems less viable since in future if we add more consumers we will have to add new web services with duplicate code for the same operation etc.

I was looking to see if there is a way to define multiple XSD's in a single web service WSDL or maybe a better way of solving this problem.

On a side note, I'd prefer to have a SOAP based web service but if this problem gets resolved using a RESTful web service in a better way then I can consider that as well.

解决方案

You don't need to have a different web service or skeleton for each client, you just need a different WSDL.

Forget about sending this XML parameter as string, it has disadvantages that I won't repeat here since you already read about it too. So you need to send it as XML.

To keep it into one web service, you create the web service so that it accept any XML as input (type <xsd:any> for the parameter). Type <xsd:any> will mean any kind of XML, but at least it would be XML that you can validate for structure, for starters.

This simplifies versioning of your web service as you don't need to change the code when adding another XML type of input but unfortunately you lose strong typing for your web service and the WSDL generated for the web service will not state anything about the structure of the XML it's expecting (any XML is too vague). Your web service skeleton will be created with a type like Object, XmlElement, XmlDocument or whatever in your code. And the same will happen with all your client stubs.

But for your clients this can be fixed from the WSDL by providing each client with a different WSDL.

For that you create a WSDL of your web service with common content for all clients, except the <types> section, and then merge this common WSDL for each client with their own XML Schema. You will then expose each WSDL on different URLs for each client.

Now the clients will be able to generate strong typed stubs based on their own particular WSDL.

When a client makes a call to your web service, you identify the client XML Schema and use it to validate the XML parameter and see if it's the expected content.

And if the above seem to much, then you can always choose a loosely typed RESTful web service. Whatever solution you choose though, just make sure that you validate the incoming XML using the schema of the client that's sending it.

这篇关于一个网络服务中是否可以有多个 xsd 映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆