英文:
JAXB / Java 11 / Tomcat - DataHandler No default constructor
问题
我在使用JAXB时遇到了一些问题,我发现有许多不同的实现方式,我之前使用了JBoss,代码运行良好(使用Metro JAXB)。现在切换到了Tomcat并且使用了com.sun.xml.bind,但是出现了问题,它无法将Base64编码的字符串转换为DataHandler。我重新编译了代码,正在尝试使用@XMLAttachmentRef注解来解决问题。
[com.sun.xml.bind.v2.ClassFactory] 在类javax.activation.DataHandler上找不到默认构造函数
java.lang.NoSuchMethodException: javax.activation.DataHandler.
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)
我理解的问题是,它失败是因为DataHandler没有一个无参构造函数。
我正在使用OSGI,我在想是否可以通过移除com.sun.xml.bind并添加一些其他的JAR包来实现JAXB的替换,以实现注入?
英文:
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
得分: 0
经过超过10小时的调试,我找到了问题的原因:
类 TypeInfoSetImpl 的方法 getTypeInfo
在这里,JAXB 在一个哈希映射中查找内置类型,它会使用 hashcode() 比较类类型,但是我的两个 javax.activation.DataHandler 使用了两个不同的类加载器,这导致它们是不同的,因此返回为 null,而类 javax.activation.DataHandler 被识别为可以使用空参数构造函数实例化的 pojo 自定义类。
删除第二个 jar 文件解决了我的问题。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论