线程“main”中的异常javax.xml.bind.JAXBException:类或其任何超类都是此上下文已知的 [英] Exception in thread "main" javax.xml.bind.JAXBException: class nor any of its super class is known to this context
问题描述
有些人可以帮助我抛出以下内容
Can some help me why below is thrown
线程main中的异常javax.xml.bind.JAXBException:class com.jaxb.model.copy.copy .Snapshot或其任何超级类都是在这种情况下已知的。
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:588)
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer。 java:482)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323)
at com.sun.xml.bind.v2.runtime.MarshallerImpl。 marshal(MarshallerImpl.java:251)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
at com.jaxb.model.copy.copy.Demo.main(Demo.java) :29)
Exception in thread "main" javax.xml.bind.JAXBException: class com.jaxb.model.copy.copy.Snapshot nor any of its super class is known to this context. at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:588) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:482) at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323) at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:251) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source) at com.jaxb.model.copy.copy.Demo.main(Demo.java:29)
演示类
package com.jaxb.model.copy.copy;
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("com.jaxb.model.copy.copy");
Snapshot snapshot = new Snapshot();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(snapshot, System.out);
}
}
Objectfactory类
Objectfactory class
package com.jaxb.model.copy.copy;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
@XmlElementDecl(namespace = "http://soma/201407",name="bar")
public JAXBElement<Foo.Detail.Bar> createBar(Foo.Detail.Bar bar) {
return new JAXBElement<Foo.Detail.Bar>(new QName("bar"), Foo.Detail.Bar.class, bar);
}
}
快照类
包com.jaxb.model.copy.copy;
Snapshot class package com.jaxb.model.copy.copy;
import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.guthyrenker.soma.ws.rest.v1.adapter.JsonDateTimeSerializer;
/**
* @author Srinivasa Kankipati
* @since 1.0
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = { "mask", "xmlns", "currentAsOf","foo" })
@XmlRootElement(name = "snapshot")
public class Snapshot
{
@XmlAttribute
public String mask;
@XmlElement
public Date currentAsOf;
@XmlAttribute
@JsonIgnore
public String xmlns;
@XmlElement(name = "foo")
private Foo foo;
public String getMask() {
return mask;
}
public void setMask(String mask) {
this.mask = mask;
}
public Date getCurrentAsOf() {
return currentAsOf;
}
public void setCurrentAsOf(Date currentAsOf) {
this.currentAsOf = currentAsOf;
}
public String getXmlns() {
return xmlns;
}
public void setXmlns(String xmlns) {
this.xmlns = xmlns;
}
public Foo getFoo() {
return foo;
}
public void setFoo(Foo foo) {
this.foo = foo;
}
}
Foo class
Foo class
package com.jaxb.model.copy.copy;
import java.math.BigDecimal;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
import com.guthyrenker.soma.ws.rest.v1.model.ObjectFactory;
import com.guthyrenker.soma.ws.rest.v1.model.WSMarketingOfferOP;
import com.guthyrenker.soma.ws.rest.v1.model.WSMarketingOfferWEB;
import com.jaxb.model.copy.Snapshot;
import com.jverstry.annotations.generics.Market;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Snapshot.class})
public class Foo {
@XmlElement
protected Detail detail;
public Detail getDetail() {
return detail;
}
public void setDetail(Detail detail) {
this.detail = detail;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "bar"})
public static class Detail
{
@XmlElementRef(name="bar")
private JAXBElement<Foo.Detail.Bar> bar;
public JAXBElement<Foo.Detail.Bar> getBar() {
return bar;
}
public void setBar(JAXBElement<Foo.Detail.Bar> bar) {
this.bar = bar;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "value" })
public static class Bar
{
@XmlAttribute
protected String baz;
@XmlValue
protected BigDecimal value;
public String getBaz() {
return baz;
}
public void setBaz(String baz) {
this.baz = baz;
}
public BigDecimal getValue() {
return value;
}
public void setValue(BigDecimal value) {
this.value = value;
}
}
}
}
推荐答案
在您当前的设置中,您的 JAXBContext
不会知道您的快照
上课。在上下文路径上创建 JAXBContext
时,JAXB将首先为从 ObjectFactory
引用的类创建元数据(或者列在 jaxb.index
文件中,然后它将提供它可以传递到的每个类。
In your current set up your JAXBContext
would not be aware of your Snapshot
class. When you create your JAXBContext
on a context path, JAXB will first create metadata for the classes referenced from the ObjectFactory
(or listed in a jaxb.index
file), then it will pull in each class it can transitively reach.
你可以执行以下操作:
JAXBContext.newInstance(ObjectFactory.class, Snapshot.class);
这篇关于线程“main”中的异常javax.xml.bind.JAXBException:类或其任何超类都是此上下文已知的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!