CXF client SOAP message formatting

2020-07-04 06:48发布

问题:

The WSDL file I used to create the CXF client has this element definition:

<xsd:element name="Rate"> 
  <xsd:simpleType>
    <xsd:restriction base="xsd:decimal">
      <xsd:totalDigits value="18" />
      <xsd:fractionDigits value="2" />
    </xsd:restriction>
  </xsd:simpleType>
</xsd:element>

However when I try to send the SOAP message, number of digits in after decimal point exceeds the maximum value. For example I get 2.48862 while expecting 2.48. In order to solve this I was planning to implement a XmlAdapter to marshall the values, however I cannot map the element in WSDL to the client because onyl class of XmlAdapter is passed to field decleration as annotation.

@XmlJavaTypeAdapter(CustomXmlAdapter.class)

There seems to be no way to inform the XmlAdapter that field must have 2 digits after decimal point.

The number of fraction digits changes from element to element. I also don't have the access to change the WSDL.

Is there a way to format these elements while observing the number of decimal points specified in the WSDL?

回答1:

I might suggest the use of xQuery on the schema referenced by the WSDL. The name and location of the schema are part of the WSDL, and can be retrieved via XQuery also.

Schemas are well-formed XML, so you will just have to determine the correct XPath selector to retrieve the desired value - in this case, that will be at a minimum something like...

//xsd:element[@name == 'Rate']//xsd:fractionDigits/@value

Which should yield the value 2.

Keep in mind that there is an XPath/XQuery interpretor built into the latest JDKs. You merely have to put some kind of front-end on it, and I believe it will handle streams quite nicely.

Doing the above will let you query the schema dynamically as you are processing a given element, allowing you, for example, to get the correct number of decimal digits when processing the Rate.