gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:06:37 28 4
gpt4 key购买 nike

我有一个名为 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

生成的工件代码如下所示

对象工厂:

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

最佳答案

这也常常导致以下异常

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 步骤。

关于java - JAXB 对象工厂不包含 JAXBElement 创建方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10853972/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com