3

我有一个名为 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 Generated Schema 创建了它的模式:

<?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

生成的工件代码如下

对象工厂:

//
// 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 的方法。我应该怎么做才能得到那个方法

4

3 回答 3

3

这也经常导致以下异常

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>

然后运行在这个新模式上创建 JAXB 工件的 xjc 步骤。

于 2012-12-19T23:11:09.887 回答
1

XJC 只会在必要时将字段创建为 JAXBElements,并且仅当映射需要一些无法以“正常”JavaBean 语义表示的元信息时才真正需要。

一个必要的例子是当一个字段是可空的并且 minOccurs=0 时。如果您定义这样的元素,您将获得一个 JAXBElement 作为字段类型而不是目标类型。这样您就可以区分“无”和“未提供”,这大概是您需要做的(否则您不会在模式中以这种方式定义它)。

于 2012-06-01T16:35:45.093 回答
0

如果要创建 MyClass 的实例。

MyClass mc = ObjectFactory.createMyClass()

如果你想从你的类创建 XML,你使用一个编组器,如果你想从一个 XML 文件创建一个 MyClass 的对象,你使用一个解组器。JEE5 文档

于 2012-06-01T16:42:02.143 回答