通过绑定文件指定 @XmlJavaTypeAdapter 类?

发布于 2024-12-10 17:56:12 字数 1951 浏览 0 评论 0原文

我有一个第三方接口,它提供与其 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奢华的一滴泪 2024-12-17 17:56:12

您需要在绑定配置中使用 而不是
例如:

<xjc:javaType name="java.lang.Boolean" xmlType="xs:boolean"
              adapter="mumble.bindings.BooleanAdapter"/>

我知道我正在回答一个老问题,但我没有足够的声誉来撰写评论。

You need to use <xjc:javaType> in your binding config instead of <jaxb:javaType>.
For example:

<xjc:javaType name="java.lang.Boolean" xmlType="xs:boolean"
              adapter="mumble.bindings.BooleanAdapter"/>

I understand I'm answering to an old question, but I don't have enough reputation to write a comment.

你丑哭了我 2024-12-17 17:56:12

我意识到,这是一个超级晚的回复,但即使是 mvv 的回复也让我很难完全理解我的意思正在做什么以及新元素在结构中适合的位置,因此我想为以后遇到此问题的任何人添加一些进一步的细节。

Per mvv,最简单的答案是更改为使用 xjc:javaType。有关使用 的详细文档,请参阅 jaxb 自定义 xjc:javaType

您还需要更改自定义适配器 (BooleanAdapter) 以实现 XmlAdapter 接口。

最终,您的绑定将如下所示:

<?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" />
            <xjc:javaType name="java.lang.Boolean" xmlType="xs:boolean"
                  adapter="mumble.bindings.BooleanAdapter" />
        </jaxb:globalBindings>
    </jaxb:bindings>
</jaxb:bindings>

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 using xjc:javaType.

You'll also need to change your custom adapter (BooleanAdapter) to implement the XmlAdapter interface.

Ultimately, your binding would instead look like this:

<?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" />
            <xjc:javaType name="java.lang.Boolean" xmlType="xs:boolean"
                  adapter="mumble.bindings.BooleanAdapter" />
        </jaxb:globalBindings>
    </jaxb:bindings>
</jaxb:bindings>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文