Question

JAX-WS requires that all classes that are transmitted have a default contructor (no-arg constructor). I do not understand that requirement because clients create their own classes based on the WSDL. IMO this requirement makes sense only for those classes that are used as input parameters of the Webservice.

Does anyone know how to circumvent that requirement?

Était-ce utile?

La solution

When you use JAX-WS you are using a JAXB implementation to serialize your java objects to XML.

So, the 'problem' is how JAXB works.

To use JAXB, you need to create a JAXBContext passing it all the classes that can be marshaled/unmarshaled. When creating the context, JAXB will check that all the given classes have a no-arg constructor. If at least one of those classes does not have this kind of constructor, the context will not be created.

Why JAXB do this? It needs this no-arg constructor ONLY when transforming from XML to Object (unmarshalling), but the issue is when you are creating the context, JAXB does not know what you want to do (marshal or unmarshal)!

Conclusion: JAXB will accept only classes that it can marshal AND unmarshal. More info here

Knowing this, what happen in JAX-WS?

When you declare a @WebMethod the parameters and return value classes will be added to a JAXB context. And is because of this that all the classes related to a web service input and output needs a no-arg constructor.

Conclusion: is JAXB fault ;-)

But what if I need to use a class that does not have a no-arg constructor?

You can use an XMLAdapter! Check this post for more info ...

Autres conseils

Does anyone know how to circumvent that requirement?

Yes - rewrite JAX-WS.

It's probably using a default ctor and reflection to populate objects, because it can't easily know about every possible ctor that someone like you might write.

This is a drawback of using someone else's framework: you have to play by their rules.

clients create their own classes based on the WSDL

I thought that's what the library helped clients do. You didn't write the code to parse and interpret the WSDL, did you?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top