问题
I have some trouble with JAXB, i found out that it exists many implementations of it, i used JBoss and the code worked well (Metro JAXB), now with tomcat and com.sun.xml.bind.. it doesn't, it cannot convert the Base64 encoded String into a DataHandler. Now i recompiled it again and i'm trying if the @XMLAttachmentRef would solve my trouble.
[com.sun.xml.bind.v2.ClassFactory] No default constructor found on class javax.activation.DataHandler
java.lang.NoSuchMethodException: javax.activation.DataHandler.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3349)
at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2553)
at com.sun.xml.bind.v2.ClassFactory.tryGetDeclaredConstructor(ClassFactory.java:107)
What i understood is that it fails because the DataHandler does not have a null args constructor.
I'm using OSGI i asked to my self if i could change the JAXB by removing com.sun.xml.bind and add some other jars to get it injected?
回答1:
So after > 10h of debugging i have found the cause:
Class TypeInfoSetImpl Method getTypeInfo Here makes JAXB a lookup into the built in types in a hashmap, it compares the class types with hashcode(), but my two javax.activation.DataHandler had two different classloaders, that causes that they are different, so it returned null and the class javax.activation.DataHandler was reconized as pojo custom class that can instantiated with null args constructor.
Deleting the secound jar resolved my problem.
来源:https://stackoverflow.com/questions/61612982/jaxb-java-11-tomcat-datahandler-no-default-constructor