我遇到一个问题,当 XSD 文件包含 double 值时,jaxb2-maven-plugin 会生成无效的源代码。
我使用 jaxb2-maven-plugin (org.codehaus.mojo) 1.5 版:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<configuration>
</configuration>
<executions>
<execution>
<id>analysis_jaxb</id>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<clearOutputDir>false</clearOutputDir>
<schemaFiles>Analysis.xsd</schemaFiles>
<packageName>xx.xx.xx.analysis</packageName>
<generateDirectory>${project.build.directory}/generated-sources/jaxb/analysis</generateDirectory>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
从以下 XSD 文件生成 Java 源代码:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="MinMax" type="MinMaxType"/>
<xs:complexType name="MinMaxType">
<xs:attribute name="min" type="xs:double" default="-INF" />
<xs:attribute name="max" type="xs:double" default="INF" />
</xs:complexType>
</xs:schema>
生成的 Java 文件包含此方法:
public double getMin() {
if (min == null) {
return -InfinityD; //UNDEFINED
} else {
return min;
}
}
字段-InfinityD
未在任何地方定义。
当使用 bool 值(例如 <xs:attribute name="minInclusive" type="xs:boolean" default="false" />
)时,默认值按预期工作。
与此相反,插件 org.jvnet.jaxb2.maven2 (maven-jaxb2-plugin) 会写成 Double.POSITIVE_INFINITY
在那条有问题的线上。
这根本不被支持吗?我是否缺少参数?
最佳答案
使用这个 XSD...
<xs:schema attributeFormDefault="unqualified"
targetNamespace="yourNameSpace"
xmlns:a="yourNameSpace"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MinMax" type="a:MinMaxType"/>
<xs:complexType name="MinMaxType">
<xs:attribute name="min" type="xs:double" default="-INF" />
<xs:attribute name="max" type="xs:double" default="INF" />
</xs:complexType>
</xs:schema>
如果你使用
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.9.0</version>
工作正常
输出:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MinMaxType")
public class MinMaxType {
@XmlAttribute(name = "min")
protected Double min;
@XmlAttribute(name = "max")
protected Double max;
/**
* Recupera il valore della proprietà min.
*
* @return
* possible object is
* {@link Double }
*
*/
public double getMin() {
if (min == null) {
return java.lang.Double.NEGATIVE_INFINITY;
} else {
return min;
}
}
/**
* Imposta il valore della proprietà min.
*
* @param value
* allowed object is
* {@link Double }
*
*/
public void setMin(Double value) {
this.min = value;
}
/**
* Recupera il valore della proprietà max.
*
* @return
* possible object is
* {@link Double }
*
*/
public double getMax() {
if (max == null) {
return java.lang.Double.POSITIVE_INFINITY;
} else {
return max;
}
}
/**
* Imposta il valore della proprietà max.
*
* @param value
* allowed object is
* {@link Double }
*
*/
public void setMax(Double value) {
this.max = value;
}
}
插件配置:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.9.0</version>
<executions>
<execution>
<id>commun-generate</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generateDirectory>${basedir}/src/main/java/</generateDirectory>
<schemaDirectory>${basedir}/src/main/resources/schema/xsd</schemaDirectory>
<strict>true</strict>
<extension>true</extension>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
使用jaxb2-maven-plugin
插件配置:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
</configuration>
<executions>
<execution>
<id>analysis_jaxb</id>
<phase>generate-sources</phase>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<clearOutputDir>false</clearOutputDir>
<schemaFiles>your.xsd</schemaFiles>
<packageName>xx.xx.xx.analysis</packageName>
<generateDirectory>generated-sources/jaxb/analysis</generateDirectory>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
输出
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MinMaxType")
public class MinMaxType {
@XmlAttribute(name = "min")
protected Double min;
@XmlAttribute(name = "max")
protected Double max;
/**
* Recupera il valore della proprietà min.
*
* @return
* possible object is
* {@link Double }
*
*/
public double getMin() {
if (min == null) {
return java.lang.Double.NEGATIVE_INFINITY;
} else {
return min;
}
}
/**
* Imposta il valore della proprietà min.
*
* @param value
* allowed object is
* {@link Double }
*
*/
public void setMin(Double value) {
this.min = value;
}
/**
* Recupera il valore della proprietà max.
*
* @return
* possible object is
* {@link Double }
*
*/
public double getMax() {
if (max == null) {
return java.lang.Double.POSITIVE_INFINITY;
} else {
return max;
}
}
/**
* Imposta il valore della proprietà max.
*
* @param value
* allowed object is
* {@link Double }
*
*/
public void setMax(Double value) {
this.max = value;
}
}
关于maven - jaxb2-maven-plugin 为默认值 =INF 的 double 生成无效源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26337076/