【问题标题】:Default Double type element to null in CXFCXF 中的默认 Double 类型元素为 null
【发布时间】:2014-12-03 16:30:13
【问题描述】:

我正在使用 CXF wsdl2java,并且我的架构中有以下 complexType:

 <complexType name="ABCType">
    <sequence>
        <element name="xxx" type="int"   maxOccurs="1"
            minOccurs="1">
        </element>
        <element name="yyy" type="double" maxOccurs="1"
            minOccurs="0">
        </element>
</complexType>

下面是生成的类:

public class ABCType
implements Serializable
{

private final static long serialVersionUID = 1L;
protected Integer xxx;
protected Double yyy;



public Double getYYY() {
    return yyy;
}


public void setYYY(Double value) {
    this.yyy= value;
}
}

当我为上面的 complexType 生成 Java 类时。我看到“yyy”属性是 Double 类型。当 yyy 元素不作为请求的一部分出现时,我如何要求 CXF 将默认的“yyy”值设为 null 而不是 0.0。

谢谢!

【问题讨论】:

  • 你能展示一下生成的Java类吗?
  • 我已经添加了上面的类,它不是真正的类,但生成的代码是相同的。

标签: jaxb cxf wsdl2java


【解决方案1】:

不确定上述代码是如何生成的。如果字段是必需的(min=1,max=1),则 cxf codegen 生成原始类型,如果是可选的(min=0 和 max=1),则生成包装器类型。并且在java中引用类型被初始化为null。因此 yyy 将为空

无论如何,如果您需要在原始类型中添加默认值,请假设为以下 xsd

<element name="xxx" type="int"   maxOccurs="1" minOccurs="1" default="4" >

您可以将默认生成器扩展添加到 cxf-codegen 插件,如下所示

                 <plugin>
                        <groupId>org.apache.cxf</groupId>
                        <artifactId>cxf-codegen-plugin</artifactId>
                        <version>3.0.1</version>
                        <executions>
                            <execution>
                                <id>generate-sources</id>
                                <phase>generate-sources</phase>
                                <configuration>
                                    <wsdlOptions>
                                        <wsdlOption>
                                            <wsdl>${basedir}/src/main/resources/mywsdl.wsdl</wsdl>
                                            <extraargs>
                                                <extraarg>-impl</extraarg>
                                                <extraarg>-verbose</extraarg>
                                                <extraarg>-xjc-Xdv</extraarg>
                                            </extraargs>
                                        </wsdlOption>
                                    </wsdlOptions>
                                </configuration>
                                <goals>
                                    <goal>wsdl2java</goal>
                                </goals>
                            </execution>
                        </executions>
                        <dependencies>
                            <dependency>
                                <groupId>org.apache.cxf.xjcplugins</groupId>
                                <artifactId>cxf-xjc-dv</artifactId>
                                <version>${cxf.version}</version>
                            </dependency>
                        </dependencies>
                    </plugin>

【讨论】:

    猜你喜欢
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多