2017-08-25 72 views
0

我们从客户处获取WSDL。 (即,我们不能改变它们。)jax ws double tostring更改格式

的类型之一的定义看起来是这样的:

<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> 

客户想要的内容t o被显示为不带十进制数字,即10等

我怀疑当从Java对象生成请求xml时,JAX-WS框架只是简单地调用Double.toString(),这将不可避免地添加一个小数点,十进制数字。

有没有办法改变这个而不能修改WSDL?为这种类型注册一些自定义数字格式化程序或类似的东西?

谢谢!

回答

0

在此期间,我找到了解决方案。它看起来是这样的:(使用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); 
} 

} 

而且你去那里。