【问题标题】:jax ws double tostring change formatjax ws double tostring 改变格式
【发布时间】:2017-08-25 06:07:46
【问题描述】:

我们从客户那里获得 WSDL。 (即,我们无法更改它们。)

其中一种类型的定义如下所示:

<complexType name="Type1">
  <complexContent>
<restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
  <sequence>
    <element name="value" minOccurs="0">
      <complexType>
    <complexContent>
      <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
        <choice>
          <element name="bigdecimal" type="{http://www.w3.org/2001/XMLSchema}double"/>
          <element name="date" type="{http://www.w3.org/2001/XMLSchema}date"/>
          <element name="string" type="{http://www.w3.org/2001/XMLSchema}string"/>
        </choice>
      </restriction>
    </complexContent>
      </complexType>
    </element>
    <element name="element2" type="{http://www.w3.org/2001/XMLSchema}integer" minOccurs="0"/>
  </sequence>
</restriction>
  </complexContent>
</complexType>

导致类似的事情

public void setBigdecimal(Double value) {
this.bigdecimal = value;
}

现在,当我们发送包含这种类型的请求时,它会生成如下内容:

<rpcOp:value>
  <rpcOp:bigdecimal>10.0</rpcOp:bigdecimal> <!-- IN SPITE OF THE NAME, THIS IS A DOUBLE VALUE! -->
  <rpcOp:string>N</rpcOp:string>
</rpcOp:value>

客户希望显示的内容不带十进制数字,即 10 等。

我怀疑JAX-WS框架在从Java对象生成request xml时,只是简单地调用了Double.toString(),必然会添加一个小数点和一个小数位。

有没有办法在不修改 WSDL 的情况下更改它?为这种类型或类似的东西注册一些自定义数字格式化程序?

谢谢!

【问题讨论】:

    标签: java web-services wsdl jax-ws number-formatting


    【解决方案1】:

    与此同时,我找到了解决方案。它看起来像这样:(使用 JAXB)

    xjc-serializable.xml:

    ...
    <jaxb:globalBindings>
    <xjc:serializable />
    <!-- ADDED THIS: -->
    <xjc:javaType name="java.lang.Double" xmlType="xs:double"
    adapter="util.MyDoubleAdapter" />
    </jaxb:globalBindings>
    ...
    

    然后是java类:

    public class MyDoubleAdapter extends XmlAdapter<String, Double> {
    
    @Override
    public String marshal(Double doubleValue) throws Exception {
        if (doubleValue == null) {
        return null;
        }
        String string = doubleValue.toString();
        if (string.endsWith(".0")) {
        string = string.replaceAll("\\.0", "");
        }
        return string;
    }
    
    public Double unmarshal(String stringValue) throws Exception {
        if (stringValue == null) {
        return null;
        }
        return Double.valueOf(stringValue);
    }
    
    }
    

    你去吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 2011-05-21
      相关资源
      最近更新 更多