1

JAXB is complaining that class X "...does not have a no-arg default constructor."

Class X in my case is one among dozens of auto-generated, final classes that indeed do not have a no-arg default constructor. It also happens to extend an abstract class that I can modify if I wish. I thought of putting an @XmlJavaTypeAdapter annotation on the abstract class but that doesn't work and I'm not sure it even makes sense. What is the best solution or workaround for this?

EDIT: Just to be clear. I cannot modify the generated classes.

3 Answers 3

1

You can add an XmlAdapter on an abstract super class (see http://blog.bdoughan.com/2012/01/jaxb-and-inhertiance-using-xmladapter.html). It is important to note that the XmlAdapter will only be applied to mapped fields/properties that reference the class and not when an instance of that class is marshalled as the root object. You will also not be able to include that class in the array of classes passed in to bootstrap the JAXBContext.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not sure I understand the "It is important to note..." part of your answer. Your blog, which I read prior to posting the question in fact, requires that the adapter knows about all the implementation classes. In my case there are about 50 classes that extend the abstract class and they could change at anytime. Perhaps I'm asking the impossible if I say I'd like JAXB to be able to marshal anything that extends the abstract class...
0

put a no-arg constructor in that class like this:

public ClassName(){
}

Comments

0

It is often the case that (because of frameworks) you need a no-args constructor. For example when working with JAXB, the beans it instantiates require a no-args constructor. What I often do is:

public MyClass(String something) {
    // do something
}

public MyClass() {
    // for frameworks!
}

This of course requires you not to put your logic in the constructor however this is almost always possible.

PS: some frameworks actually have annotations that allow for @PostConstruct calls.

3 Comments

I understand that it is needed by JAXB but I cannot simply add it myself to the auto-generated classes. It's not desirable anyway because it means that the class can be constructed without passing in something (taking your answer as an example). Although I might see if I can have the classes generated with a private no-arg constructor - that's a workaround but I was hoping JAXB could offer something itself instead.
If you can not create a no-args constructor then I'm not sure if it's possible to use JAXB, how exactly would you tell it which constructor to use and which arguments to pass in for each instance? Perhaps a fundamental rethink of the design is necessary because it seems like an odd situation?
It is an odd and inconvenient situation indeed. But sometimes unfortunately, one has to work with legacy code that cannot be altered easily, may not be accessible to you or be too costly to refactor... Really this is a question for a JAXB expert that knows of a trick or two...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.