适应嵌套域名界面& JAXB中的bean类型层次结构

时间:2017-09-05 20:46:15

标签: java jaxb jax-rs

我正在编写基于JAX-RS& amp;的REST服务。使用JAXB数据绑定遇到了一些麻烦。

尝试了几天之后,我仍然很困惑如何在引用界面的域对象之间进行调整。通过JAXB的抽象值类型层次结构。我已经开始利用XmlAdapter的道路了,但是试图在价值类型和价值之间进行转换已经走到了尽头。域界面自动。

注意:有一些公司要求使这比我预期的更棘手:对于构建工件依赖性隔离,JAXB带注释的bean不能直接或间接引用域类型({ {1}}可以;它在不同的构建工件中)。同样,依赖注入&其他支持框架无法引入。使用标准JAXB& amp; JAX-RS最符合我的要求。

域类型

XmlAdapter

JAXB-Mappable Types

public interface IBar {
    //interface not relevant to this problem
}

public final class Foo {
    private final IBar bar;

    public Foo(IBar bar) {
        this.bar = bar;
    }

    public IBar getBar() {
        return this.bar;
    }
}

FooXmlAdapter

public abstract class AbstractBarBean {
    //marker base class to help JAXB manage the hierarchy
    //this type contains no interface or fields
}

@XmlRootElement
public final class ExampleBarBean extends AbstractBarBean {
    //implementation not relevant to this problem
    //there will be an unbounded number of these implemented
    //looking to avoid having a unique XmlAdapter for each implementation
}

@XmlRootElement
public final class FooBean {

    @XmlElementRef
    private AbstractBarBean bar;

    public FooBean() { /* no-arg constructor to please jaxb */ }

    public FooBean(AbstractBarBean bar) {
        this.filter = filter;
    }

    public AbstractBarBean getBar() { return bar; }
    public void setBar(AbstractBarBean bar) { this.bar = bar; }
}

正如public final class FooAdapter extends XmlAdapter<FooBean, Foo> { @Override public FooBean marshal(Foo v) throws Exception { IBar bar = v.getBar(); //can obtain trivially //how to marshall to an AbstractBarBean automatically via JAXB?? AbstractBarBean barBean = jaxbMagicToConvertDomainToMappable(bar); return new FooBean(barBean); } @Override public Foo unmarshal(FooBean v) throws Exception { AbstractBarBean barBean = v.getBar(); //can obtain trivially //how to unmarshall to an IBar implementation automatically via JAXB?? IBar bar = jaxbMagicToConvertMappableToDomain(barBean); return new Foo(bar); } } 评论中所述,我陷入了适配器实现。

如何利用JAXB自动管理XmlAdapter&amp;之间的转换? IBar个实例?

是否有办法利用JAXBContext在AbstractBarBean&amp; IBar实施没有必须为每个新的AbstractBarBean实施编写XmlAdapter实施?

0 个答案:

没有答案