Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zero argument constructor required for superclass of class to be serialized #2076

Open
lukevd opened this issue Feb 26, 2024 · 0 comments
Open

Comments

@lukevd
Copy link

lukevd commented Feb 26, 2024

Describe the bug
If a class, which should be serialized, extends another class with no zero argument constructor, an error occurs.
To Reproduce
The description of the bug is based on the example specified here:
https://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/TheBasics
Suppose we have a class Customer which should be serialized to XML:

package example.gettingstarted;

public class Customer extends Supercustomer {
 
    private String name;
 
    public Customer() {
	super("Super");
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
}

The class Supercustomer is given below, note that it does not have a zero argument constructor:

package example.gettingstarted;

public class Supercustomer {
	String name;

	public Supercustomer(String name){
		this.name = name;
	}
}

The class Demo.java is used to serialize an object of Customer to XML:

package example.gettingstarted;
 
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import javax.xml.namespace.QName;
 
public class Demo {
 
    public static void main(String[] args) throws JAXBException {
 
        // Step 1 - Create the Domain Model
 
        Customer customer = new Customer();
        customer.setName("Jane Doe");
 
        // Step 2 - Convert the Domain Model to XML
 
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
 
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 
        JAXBElement<Customer> jaxbElement = new JAXBElement<Customer>(new QName(null, "customer"), Customer.class, customer);
        marshaller.marshal(jaxbElement, System.out);
 
    }
 
}

On execution, the following error occurs:

[Exception [EclipseLink-50001] (Eclipse Persistence Services - 4.0.2.v202306161219): org.eclipse.persistence.exceptions.JAXBException
Exception Description: The class example.gettingstarted.Supercustomer requires a zero argument constructor or a specified factory method.  Note that non-static inner classes do not have zero argument constructors and are not supported.]
        at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1173)
        at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:208)
        at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:167)
        at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:154)
        at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:114)
        at org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(JAXBContextFactory.java:104)
        at org.eclipse.persistence.jaxb.XMLBindingContextFactory.createContext(XMLBindingContextFactory.java:43)
        at jakarta.xml.bind.ContextFinder.find(ContextFinder.java:368)
        at jakarta.xml.bind.JAXBContext.newInstance(JAXBContext.java:605)
        at jakarta.xml.bind.JAXBContext.newInstance(JAXBContext.java:546)
        at example.gettingstarted.Demo.main(Demo.java:20)
Caused by: Exception [EclipseLink-50001] (Eclipse Persistence Services - 4.0.2.v202306161219): org.eclipse.persistence.exceptions.JAXBException
Exception Description: The class example.gettingstarted.Supercustomer requires a zero argument constructor or a specified factory method.  Note that non-static inner classes do not have zero argument constructors and are not supported.
        at org.eclipse.persistence.exceptions.JAXBException.factoryMethodOrConstructorRequired(JAXBException.java:150)
        at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.finalizeProperties(AnnotationsProcessor.java:961)
        at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.processClassesAndProperties(AnnotationsProcessor.java:309)
        at org.eclipse.persistence.jaxb.compiler.Generator.<init>(Generator.java:154)
        at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1169)
        ... 10 more
  • EclipseLink version: 4.0.2
  • Java/JDK version: 17

Expected behavior
The class, which extends another class, can be serialized, even if the superclass does not have a zero argument constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant