通过绑定文件指定 @XmlJavaTypeAdapter 类?
我有一个第三方接口,它提供与其 API 匹配的 xsd 文件。它们的一些映射并不完全是 Java 的,通常的布尔值是 0 和 0。 1 :-(
我想使用绑定文件来为我的 BooleanAdapter 指定 @XmlJavaTypeAdapter 类,但到目前为止还没有什么乐趣。
绑定文件:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns="http://java.sun.com/xml/ns/jaxws"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">
<jaxb:bindings schemaLocation="GetUserDetailsResponse.xsd" node="/xs:schema" >
<jaxb:globalBindings underscoreBinding="asWordSeparator" >
<jaxb:serializable uid="1" />
<jaxb:javaType name="java.lang.Boolean" xmlType="xs:boolean"
printMethod="mumble.bindings.BooleanAdapter.marshall"
parseMethod="mumble.bindings.BooleanAdapter.unmarshall" />
</jaxb:globalBindings>
</jaxb:bindings>
</jaxb:bindings>
由于我使用的是 Maven,因此 POM 中的相关位:
<strict>false</strict>
<extension>true</extension>
<verbose>true</verbose>
<enableWrapperStyle>false</enableWrapperStyle>
<enableAsyncMapping>false</enableAsyncMapping>
我已经切换了enableWrapperStyle并且没有变化
我最终得到的是错误类型的生成适配器:
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class Adapter1
extends XmlAdapter<String, Boolean>{
public Boolean unmarshal(String value) {
return (mumble.bindings.BooleanAdapter.unmarshall(value));
}
public String marshal(Boolean value) {
return (mumble.bindings.BooleanAdapter.marshall(value));
}
}
是否有一些绑定文件魔术我可以用来摆脱生成的包装器并直接使用BooleanAdapter?
I have a 3rd party interface that supplies xsd files that matches their API. Some of their mappings are not quite Java, the usual boolean as 0 & 1 :-(
I'd like to use a bindings file to specify the @XmlJavaTypeAdapter class for my BooleanAdapter, but so far no joy.
The bindings file:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns="http://java.sun.com/xml/ns/jaxws"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.0">
<jaxb:bindings schemaLocation="GetUserDetailsResponse.xsd" node="/xs:schema" >
<jaxb:globalBindings underscoreBinding="asWordSeparator" >
<jaxb:serializable uid="1" />
<jaxb:javaType name="java.lang.Boolean" xmlType="xs:boolean"
printMethod="mumble.bindings.BooleanAdapter.marshall"
parseMethod="mumble.bindings.BooleanAdapter.unmarshall" />
</jaxb:globalBindings>
</jaxb:bindings>
</jaxb:bindings>
And since I'm using maven the relevant bit from the POM:
<strict>false</strict>
<extension>true</extension>
<verbose>true</verbose>
<enableWrapperStyle>false</enableWrapperStyle>
<enableAsyncMapping>false</enableAsyncMapping>
I've toggled enableWrapperStyle and no change
What I end-up with is a generated Adapter of the wrong type:
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class Adapter1
extends XmlAdapter<String, Boolean>{
public Boolean unmarshal(String value) {
return (mumble.bindings.BooleanAdapter.unmarshall(value));
}
public String marshal(Boolean value) {
return (mumble.bindings.BooleanAdapter.marshall(value));
}
}
Is there some bindings file magic I can use to get rid of the generated wrapper and use the BooleanAdapter directly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在绑定配置中使用
而不是
。例如:
我知道我正在回答一个老问题,但我没有足够的声誉来撰写评论。
You need to use
<xjc:javaType>
in your binding config instead of<jaxb:javaType>
.For example:
I understand I'm answering to an old question, but I don't have enough reputation to write a comment.
我意识到,这是一个超级晚的回复,但即使是 mvv 的回复也让我很难完全理解我的意思正在做什么以及新元素在结构中适合的位置,因此我想为以后遇到此问题的任何人添加一些进一步的细节。
Per mvv,最简单的答案是更改为使用
xjc:javaType
。有关使用的详细文档,请参阅 jaxb 自定义 xjc:javaType
。您还需要更改自定义适配器 (BooleanAdapter) 以实现 XmlAdapter 接口。
最终,您的绑定将如下所示:
It's a super late response, I realize, but even mvv's response left me struggling to totally grasp what I was doing and where in the structure the new element fit, so I wanted to add some further detail for anyone coming across this later.
Per mvv, the easiest answer is to change to using the
xjc:javaType
. See jaxb customization for the detailed documentation on usingxjc:javaType
.You'll also need to change your custom adapter (BooleanAdapter) to implement the XmlAdapter interface.
Ultimately, your binding would instead look like this: