JAXB无法处理接口 [英] JAXB Can't handle interfaces

查看:161
本文介绍了JAXB无法处理接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题已被问过一百万次,但没有一个解决方案对我有用。这是我的示例实现

I think this question has been asked like a million times, but none of solutions suggested worked for me. Here is my sample implementation

public class FooImpl2 implements Foo {
    private int a = 100 ;
    private String b = "I am FooImpl2";
    private boolean c;

    public int getA() {
        return a;
    }
    public void setA(int a) {
        this.a = a;
    }
    public String getB() {
        return b;
    }
    public void setB(String b) {
        this.b = b;
    }
    public boolean isC() {
        return c;
    }
    public void setC(boolean c) {
        this.c = c;
    }

}

@XmlRootElement
@XmlSeeAlso({FooImpl1.class, FooImpl2.class})
public interface Foo {}

public class FooImpl1 implements Foo {    
    private int x;
    private String y ="I am FooImpl1";
    private boolean z;

    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public String getY() {
        return y;
    }
    public void setY(String y) {
        this.y = y;
    }
    public boolean isZ() {
        return z;
    }
    public void setZ(boolean z) {
        this.z = z;
    }        
}

@XmlRootElement
public class Response{

    private Foo foo;

    @XmlElement(type=Object.class)
    public Foo getFoo() {
        return foo;
    }

    public void setFoo(Foo foo) {
        this.foo = foo;
    }

}

public class SimpleResource {    
    @Path("foo/{val}") @Produces({"application/json"}) @GET
    public FooAdapter getFoo(@QueryParam("val") int val) {
        FooAdapter ret = new FooAdapter();
        if(val % 2 == 0) {
            ret.setFoo(new FooImpl2());
        } else {
            ret.setFoo(new FooImpl1());
        }

        return ret;
    }

我总是得到以下异常


com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:$ count of
IllegalAnnotationExceptions
com.abc.objectsToReturn.Foo是
界面,

com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions com.abc.objectsToReturn.Foo is an interface,

任何人都可以帮我找出正确的解决方案

can any one help me to figure out right solution

推荐答案

这不是一个真正的接口问题,您只需要改变引导JAXBContext的方式。

This isn't really an interface issue, you just need to change the way you bootstrap your JAXBContext.

如果您将其更改为以下内容:

If you change it to the following:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Response.class, FooImpl1.class, FooImpl2.class);

        Response response = new Response();
        FooImpl1 foo = new FooImpl1();
        response.setFoo(foo);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(response, System.out);
    }
}

然后你会得到以下输出(任何JAXB实施: Metro MOXy 等):

Then you will get the following output (with any JAXB implementation: Metro, MOXy, etc):

<response>
   <foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="fooImpl1">
      <x>0</x>
      <y>I am FooImpl1</y>
      <z>false</z>
   </foo>
</response>

MOXy JAXB允许您的整个模型成为接口,结帐:

MOXy JAXB allows your entire model to be interfaces, checkout:

  • http://bdoughan.blogspot.com/2010/07/moxy-jaxb-map-interfaces-to-xml.html

我还有一篇博文,可能与你想要建立的内容有关:

I also have a blog post that may be relevant to what you are trying to build:

  • http://bdoughan.blogspot.com/2010/08/using-xmlanyelement-to-build-generic.html

这篇关于JAXB无法处理接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆