JAXB Object factory does not contain JAXBElement creation method
jakarta-ee, java, jaxb
Solution
This also often results in the following exception
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)
The problem lies in your schema, change it so it has an outer tag. This will then generate code that has an @XmlRootElement annotation in it.
Change the schema like so:
<?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>
Then run the xjc step that creates JAXB artifacts on this new schema.
Problem
I have a Class named MYClass whose code is given below ``` package com.rest; public class MyClass { private String var; public String getVar() { return var; } public void setVar(String var) { this.var = var; } } ``` I have created its schema using 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> ``` Then, i have created JAXB artifacts by schema using ``` xjc -d <my_source_dir>\ -p com.rest.generated <my_generated_schema>.xsd ``` The generated artifacts code is given below 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(); } } ``` and MyClass.java is ``` // // 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> * <complexType name="myClass"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="var" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/> * </sequence> * </restriction> * </complexContent> * </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; } } ``` Problem : Cannot Found a method which creates JAXBElement. What should i do to get that method