JAXB对象工厂不包含JAXBElement创建方法

时间:2012-06-01 16:21:25

标签: java java-ee jaxb

我有一个名为MYClass的类,其代码在下面给出

package com.rest;


public class MyClass {

    private String var;

    public String getVar() {
        return var;
    }

    public void setVar(String var) {
        this.var = var;
    }
}

我使用schemagen ../src/com/rest/MyClass.java创建了它的架构 生成的架构:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="myClass">
    <xs:sequence>
      <xs:element name="var" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

然后,我使用

按模式创建了JAXB工件
xjc -d <my_source_dir>\ -p com.rest.generated <my_generated_schema>.xsd

生成的工件代码如下所示

ObjectFactory:

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2012.06.01 at 08:56:31 PM PKT 
//


package com.rest.generated;

import javax.xml.bind.annotation.XmlRegistry;


/**
 * This object contains factory methods for each 
 * Java content interface and Java element interface 
 * generated in the com.rest.generated package. 
 * <p>An ObjectFactory allows you to programatically 
 * construct new instances of the Java representation 
 * for XML content. The Java representation of XML 
 * content can consist of schema derived interfaces 
 * and classes representing the binding of schema 
 * type definitions, element declarations and model 
 * groups.  Factory methods for each of these are 
 * provided in this class.
 * 
 */
@XmlRegistry
public class ObjectFactory {


    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.rest.generated
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link MyClass }
     * 
     */
    public MyClass createMyClass() {
        return new MyClass();
    }

}

和MyClass.java是

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
// Any modifications to this file will be lost upon recompilation of the source schema. 
// Generated on: 2012.06.01 at 08:56:31 PM PKT 
//


package com.rest.generated;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * <p>Java class for myClass complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="myClass">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="var" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "myClass", propOrder = {
    "var"
})
public class MyClass {

    protected String var;

    /**
     * Gets the value of the var property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getVar() {
        return var;
    }

    /**
     * Sets the value of the var property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setVar(String value) {
        this.var = value;
    }

}

问题:找不到创建JAXBElement的方法。我该怎么做才能获得该方法

3 个答案:

答案 0 :(得分:1)

如果有必要,XJC只会将字段创建为JAXBElements,并且只有在映射需要一些无法用“普通”JavaBean语义表达的元信息时才需要它。

何时需要这样的一个例子是当一个字段都是nillable并且minOccurs = 0时。如果您定义这样的元素,您将获得JAXBElement作为字段类型而不是目标类型。这样你可以区分“nil”和“not provided”,这可能是你需要做的(否则你就不会在模式中用那种方式定义)。

答案 1 :(得分:1)

这通常也会导致以下异常

org.jboss.resteasy.plugins.providers.jaxb.JAXBMarshalException: The method createclass <package name here>.<class name here>() was not found in the object Factory!
    at org.jboss.resteasy.plugins.providers.jaxb.JAXBXmlTypeProvider.wrapInJAXBElement(JAXBXmlTypeProvider.java:175)
    at org.jboss.resteasy.plugins.providers.jaxb.JAXBXmlTypeProvider.writeTo(JAXBXmlTypeProvider.java:74)
    at org.jboss.resteasy.core.interception.MessageBodyWriterContextImpl.proceed(MessageBodyWriterContextImpl.java:117)

问题在于您的架构,更改它以使其具有外部标记。然后,这将生成包含@XmlRootElement注释的代码。

更改架构,如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="myClass">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="var" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  </xs:element>
</xs:schema>

然后运行xjc步骤,在此新架构上创建JAXB工件。

答案 2 :(得分:0)

如果你想创建MyClass的实例。

MyClass mc = ObjectFactory.createMyClass()

如果要从类中创建XML,则使用编组器,如果要从XML文件创建MyClass对象,则使用unmarshaller。JEE5 doc