Jaxb 编组器和泛型(2)

发布于 2024-09-15 19:00:45 字数 721 浏览 7 评论 0原文

有类型:

class A{}

@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlType(propOrder = {"obj"})
@XmlRootElement(name = "response")
public class B<T extends A> extends A{
  private T obj;

  @XmlElement(required = true)
  public T getObj() {
    return obj;
  }
}

当我尝试编组这个时,我收到错误:

org.springframework.oxm.MarshallingFailureException: JAXB marshalling exception; nested exception is javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "com.my.B" as an element because it is missing an @XmlRootElement annotation]

jaxbMarshaller 是否可以与泛型一起使用? 有什么想法吗?

谢谢

there are types:

class A{}

@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlType(propOrder = {"obj"})
@XmlRootElement(name = "response")
public class B<T extends A> extends A{
  private T obj;

  @XmlElement(required = true)
  public T getObj() {
    return obj;
  }
}

When i'm trying to marshal this i get an error:

org.springframework.oxm.MarshallingFailureException: JAXB marshalling exception; nested exception is javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "com.my.B" as an element because it is missing an @XmlRootElement annotation]

Does jaxbMarshaller work with generic?
Any ideas?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

生来就爱笑 2024-09-22 19:00:45

您的 JAXBContext 是如何创建的?您需要确保它了解 B.class。您可能需要使用@XmlSeeAlso 注释。

给出以下内容:

public class A {

}

和:

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlType(propOrder = {"obj"})
@XmlRootElement(name = "response")
public class B<T extends A> extends A {

  private T obj;

  @XmlElement(required = true)  
  public T getObj() {  
    return obj;  
  }

  public void setObj(T obj) {
      this.obj = obj;
  }

}

当我跑步时:

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(B.class);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        B b = new B();
        b.setObj(new A());
        marshaller.marshal(b, System.out);

    }

}

我得到:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
    <obj/>
</response>

当我跑步时:

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(B.class);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        B b = new B();
        b.setObj(new B());
        marshaller.marshal(b, System.out);

    }

}

我得到:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
    <obj xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="b"/>
</response>

How is your JAXBContext getting created? You will need to ensure that it is aware of B.class. You may need to use the @XmlSeeAlso annotation.

Given the following:

public class A {

}

and:

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlType(propOrder = {"obj"})
@XmlRootElement(name = "response")
public class B<T extends A> extends A {

  private T obj;

  @XmlElement(required = true)  
  public T getObj() {  
    return obj;  
  }

  public void setObj(T obj) {
      this.obj = obj;
  }

}

When I run:

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(B.class);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        B b = new B();
        b.setObj(new A());
        marshaller.marshal(b, System.out);

    }

}

I get:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
    <obj/>
</response>

And when I run:

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(B.class);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        B b = new B();
        b.setObj(new B());
        marshaller.marshal(b, System.out);

    }

}

I get:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
    <obj xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="b"/>
</response>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文