如何用零填充 xsd:decimal?

How to pad an xsd:decimal with zeros?

我们正在使用 CXF 与带有一些奇怪行为的外部 SOAP 接口对话。界面期望 xsd:decimal 被 left 填充为最多 15 位的零。所以23会变成000000000000023

如何使用 CXF 实现此填充?

自己解决了。我为此创建了一个 XmlJavaTypeAdapter,然后我通过 @XmlJavaTypeAdapter 注释使用它。

public class XmlDecimalAdapter extends XmlAdapter< String, BigDecimal > 
{

   ///////////////////////////////////////////////////////////////////////////////////////////////////////////

   @Override
   public String marshal( BigDecimal value ) throws Exception
   {
      final String  stringRepresentation = value.toString();

      if( value.compareTo( BigDecimal.ZERO ) < 0 ){
         String result = "-00000000000000";
         return result.substring( 0, result.length() - stringRepresentation.length() + 1 ) + stringRepresentation.substring( 1 );
      }
      String result = "000000000000000";
      return result.substring( 0, result.length() - stringRepresentation.length() ) + stringRepresentation;
   }

   ///////////////////////////////////////////////////////////////////////////////////////////////////////////

   @Override
   public BigDecimal unmarshal( String value ) throws Exception
   {
      if( value.equals( "000000000000000" ) ){
         return BigDecimal.ZERO;
      }   
      if( value.startsWith( "-")  ){
         return new BigDecimal( value.replaceFirst( "^-0*", "-" ) );         
      }
      return new BigDecimal( value.replaceFirst( "^0*", "" ) );         
   }
}