I’m developing a little web service and ran into a detail I don’t understand.
Thats a part of my Service Endpoint Interface:
@WebService
public interface ScriptStarterInterface {
String getResult(@WebParam(name="newFile") FileContainer newFile);
}
There is an implemantation of it, too. 😉
Now when I starteded the Server, following Exception occured:
Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
some.package.FileContainer does not have a no-arg default constructor.
I added the no-arg constructor to FileContainer and now everything works fine. What I do not understand is: Why do I have to provide this constructor?
All the oracle pages I stumbled upon just say that I have to provide one, but never why. 🙁
A no-arg constructor is required because JAXB creates instances of objects by using the
newInstance()method ofjava.lang.Class, which doesn’t allow the specifying of arguments. (We can know it is JAXB that has the problem because of the package name of that exception.) While it is possible to create object instances by getting the appropriatejava.lang.reflect.Constructorinstance and then providing the right arguments there, that’s much more complex and so is usually reserved for very sophisticated frameworks (e.g., Spring). Simple code uses the simple API, which requires a no-arg constructor; that’s good enough for most cases where you’d want to programatically create objects anyway (and for JAXB, the complex cases are dealt with through registering factory objects, which themselves have to have no-arg constructors but which can make objects with much more complexity).