开发者

Mapping types from two WSDLs to one Java class

I'm using two web services from one company which each have their own wsdl. I'm using wsimport to generate the java classes.

I found that there is a lot of overlap in the two wsdl's but the used names are different. For example when an error occurs both services return a list of error messages. But the two services use different names for the returned lists. See two subsections of the wsdl's

  .
  .
  <s:element name="LoadResponse">
    <s:complexType>
      <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="LoadResult" type="tns:ArrayOfMessageOfHierarchyLoadMessage" />
        <s:element minOccurs="0" maxOccurs="1" name="hierarchy" type="tns:Hierarchy" />
      </s:sequence>
    </s:complexType>
  </s:element>
  <s:complexType name="ArrayOfHierarchyLoadMessage">
    <s:sequence>
      <s:element minOccurs="0" maxOccurs="unbounded" name="HierarchyLoadMessage" nillable="true" type="tns:HierarchyLoadMessage" />
    </s:sequence>
  </s:complexType>
  <s:complexType name="HierarchyLoadMessage">
    <s:sequence>
      <s:element minOccurs="1" maxOccurs="1" name="Type" type="tns:MessageType" />
      <s:element minOccurs="0" maxOccurs="1" name="Text" type="s:string" />
      <s:element minOccurs="0" maxOccurs="1" name="Parameters" type="tns:ArrayOfString" />
    </s:sequence>
  </s:complexType>
  .
  .

and the subsection of the other wsdl

  .
  .
  <s:element name="SearchResponse">
    <s:complexType>
      <s:sequence>
        <s:element minOccurs="0" maxOccurs="1" name="SearchResult" type="tns:ArrayOfMessageOfErrorCodes" />
        <s:element minOccurs="0" maxOccurs="1" name="data" type="tns:FinderData" />
      </s:sequence>
    </s:complexType>
  </s:element>
  <s:complexType name="ArrayOfErrorCodes">
    <s:sequence>
      <s:element minOccurs="0" maxOccurs="unbounded" name="ErrorCodes" nillable="true" type="tns:ErrorCodes" />
    </s:sequence>
  </s:complexType>
  <s:complexType name="ErrorCodes">
    <s:sequence>
      <s:element minOccurs="1" maxOc开发者_Go百科curs="1" name="Type" type="tns:MessageType" />
      <s:element minOccurs="0" maxOccurs="1" name="Text" type="s:string" />
      <s:element minOccurs="0" maxOccurs="1" name="Parameters" type="tns:ArrayOfString" />
    </s:sequence>
  </s:complexType>
  .
  .

As you can see the overlapping types are

ArrayOfMessageOfHierarchyLoadMessage and ArrayOfMessageOfHierarchyLoadMessage
MessageOfHierarchyLoadMessage and MessageOfErrorCodes

Can you tell me if it is possible, using binding files, to map the identical types in the two wsdl's such wsimport will only generate one java class for it?


The question you need to ask yourself is: Is it a good idea to only have one class which applies to two different web services? If you have a single class, what happens if one of the companies changes its service. You not only have to modify the interface to their ws, but the second companies interface as well.

You could take the view that you would like to factor out as much code as possible, but tying the two web services together like this may not be a good idea in the long term. The interfaces may move apart over time. I think it is acceptable to duplicate code in this instance.

You've probably already thought of this, but what I would do is use an Adaptor pattern. So you would have three classes: the generic class (GenericErrorList) used everywhere in your code; and two which are generated from the respective wsdls(CompanyOneErrorList and CompanyTwoErrorList). There is a conversion method which takes a CompanyOneErrorList and creates a GenericErrorList. Same for the other company.

Unless you have a really good reason to tie the two implementations together, I would avoid doing so.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜