[英]JAXB not adding type to @XMLJavaTypeAdapter annotation
I have been given an XSD which contains a xs:integer
field.我得到了一个 XSD ,其中包含一个xs:integer
字段。 I want the generated code to use an int
, so I can apply an existing interface to it.我希望生成的代码使用int
,因此我可以将现有接口应用于它。 (the field is required and will never exceed the 32-bit range, it should have been an xs:int
). (该字段是必需的,并且永远不会超过 32 位范围,它应该是一个xs:int
)。
Using XJB I can change the type in the generated code.使用 XJB 我可以更改生成代码中的类型。 But this gives me an error when creating a JAXBContext
.但这在创建JAXBContext
时给了我一个错误。
<bindings
xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
<bindings schemaLocation="mySchema.xsd">
<bindings node="//xsd:element[@name='myField']">
<!-- either with jaxb -->
<property name="myField">
<baseType name="int">
<javaType name="int" />
</baseType>
</property>
<!-- or with xjc-->
<xjc:javaType name="int"/>
</bindings>
</bindings>
</bindings>
this generates the following code这会生成以下代码
public class MyClass {
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(value = Adapter1.class)
@XmlSchemaType(name = "integer")
protected int myField;
// ...
}
While this works for code-generation, it fails for marshalling and throws the following exception when creating a context.虽然这适用于代码生成,但它无法编组并在创建上下文时引发以下异常。
Adapter myPackage.Adapter1.class is not applicable to the field type int.
this problem is related to the following location:
at @javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(type=javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter$DEFAULT.class, value=mySchemaPackage.Adapter1.class)
if I manually add a type-parameter to the annotation the problem is solved, but I cannot find a way to make JAXB do this for me.如果我手动将类型参数添加到注释中,问题就解决了,但我找不到让 JAXB 为我执行此操作的方法。
@XmlJavaTypeAdapter(value = Adapter1.class, type = int.class)
Using the JAXB2-Basics -plugin I managed to override the generated annotation.使用JAXB2-Basics -plugin 我设法覆盖了生成的注释。 I decided to use a named adapter instead of the dynamically generated Adapter1.class
, but the implementation of IntAdapter
is the same XmlAdapter<String, Integer>
as JAXB would generate.我决定使用命名适配器而不是动态生成的Adapter1.class
,但 IntAdapter 的实现与IntAdapter
将生成的XmlAdapter<String, Integer>
相同。
<bindings
xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:annox="http://annox.dev.java.net">
<bindings schemaLocation="mySchema.xsd">
<bindings node="//xsd:element[@name='myField']">
<annox:annotate target="field">
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value=myCodePackage.IntAdapter.class, type=int.class)
</annox:annotate>
<xjc:javaType name="int" adapter="myCodePackage.IntAdapter"/>
</bindings>
</bindings>
</bindings>
and now the code is generated like现在生成的代码就像
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(value = IntAdapter.class, type = int.class)
@XmlSchemaType(name = "integer")
protected int myField;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.