Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB Object factory does not contain JAXBElement creation method

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>
 * &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;
    }

}

Problem : Cannot Found a method which creates JAXBElement. What should i do to get that method

like image 274
Zeeshan Avatar asked Oct 26 '25 06:10

Zeeshan


2 Answers

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.

like image 86
aerobiotic Avatar answered Oct 28 '25 21:10

aerobiotic


XJC will only create the fields as JAXBElements if it's necessary, and it's really only necessary when the mapping needs some meta-information that can't be expressed in "normal" JavaBean semantics.

An example of when this is necessary is when a field is both nillable and minOccurs=0. If you define an element like that, you'll get a JAXBElement as the field type instead of the target type. That's so you can differentiate between "nil" and "not provided," which presumably you need to do (else you wouldn't have defined it that way in the schema).

like image 30
Jonathan W Avatar answered Oct 28 '25 22:10

Jonathan W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!