设置要使用的 JAXB 上下文工厂初始化类 [英] Set the JAXB context factory initialization class to be used

查看:41
本文介绍了设置要使用的 JAXB 上下文工厂初始化类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已更新我们的项目(基于 Websphere 8.5 的 Java EE)以使用公司内部框架的新版本(以及 Ejb 3.x 部署描述符,而不是 2.x 部署描述符).从那时起,我的集成测试失败,出现以下异常:

I have updated our projects (Java EE based running on Websphere 8.5) to use a new release of a company internal framework (and Ejb 3.x deployment descriptors rather than the 2.x ones). Since then my integration Tests fail with the following exception:

 [java.lang.ClassNotFoundException: com.ibm.xml.xlxp2.jaxb.JAXBContextFactory]

我可以使用以前的框架版本构建应用程序,并且一切正常.在调试时,我注意到在 ContextFinder (javax.xml.bind) 中有两种不同的行为:

I can build the application with the previous framework release and everything works fine. While debugging i noticed that within the ContextFinder (javax.xml.bind) there are two different behaviours:

  1. 上一版本(一切正常):不同的地方都没有提供工厂类,因此默认工厂类被加载,即 com.sun.xml.internal.bind.v2.ContextFactory(定义为类内的字符串常量).

  1. Previous Version (Everything works just fine): None of the different places brings up a factory class so the default factory class gets loaded which is com.sun.xml.internal.bind.v2.ContextFactory (defined as String constant within the class).

升级版本 (ClassNotFound):有一个资源META-INF/services/javax.xml.bind.JAXBContext"正在成功加载,第一行读取使 ContextFinder 尝试加载com.ibm.xml.xlxp2.jaxb.JAXBContextFactory"导致错误.

Upgraded Version (ClassNotFound): There is a resource "META-INF/services/javax.xml.bind.JAXBContext" beeing loaded successfully and the first line read makes the ContextFinder attempt to load "com.ibm.xml.xlxp2.jaxb.JAXBContextFactory" which causes the error.

我现在有两个问题:

  1. 那个资源是什么类型的?因为在我们的 EAR 中有两个 WAR 并且这两个 WAR 中没有一个包含文件夹服务META-INF 目录.

  1. What sort is that resource? Because inside our EAR there is two WARs and none of those two contains a folder services in its META-INF directory.

否则该值从何而来?因为文件差异显示我没有新的或更改的属性文件.

Where could that value be from otherwise? Because a filediff showed me no new or changed properties files.

无需说我将阅读有关 JAXB 配置可能性的所有内容,但是如果您对可能出现的问题有初步了解或帮助我解决该资源(我必须寻找它是一个真实的文件吗?)我很感激.非常感谢!

No need to say i am going to read all about the JAXB configuration possibilities but if you have first insights on what could have gone wrong or help me out with that resource (is it a real file i have to look for?) id appreciate a lot. Many Thanks!

编辑(根据评论输入/问题):

出于好奇,您的框架是否包含 JAXB JAR?旧版本的框架是否包含 jaxb.properties?

确实(我有点惊讶)该框架在 EAR 中有一个定制的 eclipselink-2.4.1-.jar,其中包括一个 JAXB 实现和一个 jaxb.properties 文件,该文件在两个版本中都显示了以下条目(一个找到工厂以及抛出异常的工厂):

Indeed (i am a bit surprised) the framework has a customized eclipselink-2.4.1-.jar inside the EAR that includes both a JAXB implementation and a jaxb.properties file that shows the following entry in both versions (the one that finds the factory as well as in the one that throws the exception):

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

我认为这与当前问题无关,因为罐子在两个 EAR(运行的那个/预期的那个)中保持完全相同

I think this is has nothing to do with the current issue since the jar stayed exactly the same in both EARs (the one that runs/ the one with the expection)

我也不清楚为什么旧版本的框架会选择 com.sun 实现

有一个类 javax.xml.bind.ContextFinder 负责初始化 JAXBContextFactory.此类在各个位置搜索 jaxb.properties 文件或javax.xml.bind.JAXBContext"资源的存在.如果所有这些地方都没有显示要使用哪个上下文工厂,则加载了一个默认工厂,该工厂在类本身中进行了硬编码:

There is a class javax.xml.bind.ContextFinder which is responsible for initializing the JAXBContextFactory. This class searches various placess for the existance of a jaxb.properties file or a "javax.xml.bind.JAXBContext" resource. If ALL of those places dont show up which Context Factory to use there is a deault factory loaded which is hardcoded in the class itself:

private static final String PLATFORM_DEFAULT_FACTORY_CLASS = "com.sun.xml.internal.bind.v2.ContextFactory";

现在回到我的问题:

使用先前版本的框架(和 EJB 2.x 部署描述符)构建一切正常).在调试时,我可以看到没有找到配置,因此加载了上述默认工厂.

Building with the previous version of the framework (and EJB 2.x deployment descriptors) everything works fine). While debugging i can see that there is no configuration found and thatfore above mentioned default factory is loaded.

使用新版本的框架(和 EJB 3.x 部署描述符,以便我可以部署)构建只有一个 TESTCASE 失败,但其余功能有效(就像我可以向我们的 web 服务发送请求,它们不会触发任何错误).在调试时,我可以看到找到了一个配置.此资源名为META-INF/services/javax.xml.bind.JAXBContext".以下是此资源如何导致尝试加载com.ibm.xml.xlxp2.jaxb.JAXBContextFactory"的最重要的几行,然后抛出 ClassNotFoundException.这是提到的 javax.xml.bind.ContextFinder 类的简化源:

Building with the new version of the framework (and EJB 3.x deployment descriptors so i can deploy) ONLY A TESTCASE fails but the rest of the functionality works (like i can send requests to our webservice and they dont trigger any errors). While debugging i can see that there is a configuration found. This resource is named "META-INF/services/javax.xml.bind.JAXBContext". Here are the most important lines of how this resource leads to the attempt to load 'com.ibm.xml.xlxp2.jaxb.JAXBContextFactory' which then throws the ClassNotFoundException. This is simplified source of the mentioned javax.xml.bind.ContextFinder class:

URL resourceURL = ClassLoader.getSystemResource("META-INF/services/javax.xml.bind.JAXBContext");

BufferedReader r = new BufferedReader(new InputStreamReader(resourceURL.openStream(), "UTF-8"));

String factoryClassName = r.readLine().trim();  

字段 factoryClassName 现在具有值 'com.ibm.xml.xlxp2.jaxb.JAXBContextFactory'

The field factoryClassName now has the value 'com.ibm.xml.xlxp2.jaxb.JAXBContextFactory'

因为这已经成为一个超级大的问题,我还将增加悬赏:)我将为此工作一整天,如果有任何消息,请告诉您.

Because this has become a super lager question i will also add a bounty :) I will work the entire day on this and let you know if there is any news.

更新/解决方案

这个问题已经解决了.最初的问题是因为复杂构建的多模型 maven 项目的错误配置,其中一个依赖项使用了自定义 eclipse 链接 jar 的更新版本,其中包含在发生错误的组件中不可用的 JAXBFactory 定义.在大多数情况下,设置 JAXB 上下文工厂是使用包含相同定义的 jaxb.properie 文件或 JAXBContext 文件进行配置的.相应 JAXBContextFactory 的详细加载过程发生在 javax.xml.bind.ContextFinder 中.

This question has been solved. The original problem has occured because misconfiguration of complexly build multi model maven projects which one dependency used a updated version of a customized eclipse link jar that contained a definition for a JAXBFactory not available in the component where the error occured. Setting the JAXB context factory in most cases is configured with a jaxb.propertie file or JAXBContext file that contains the same definition. Detailed loading process of the appropriate JAXBContextFactory happens in javax.xml.bind.ContextFinder.

错误尚未解决(事实上超过 4 个主要的 EE/SE 应用程序导致错误)并且没有通用的答案,但是定义的 JAXBContextFactorys 必须存在于您的类路径中(哇,真是奇迹...)所以你要么有一个 ClassNotFound 错误,因为你缺少资源(这就是实际原因),要么因为你在上面提到的任何属性文件中定义了错误的 JAXBContextFactory ,其中包含根据以下答案的定义.

The error has not yet been solved (during the fact over 4 major EE/SE Applications lead to the error) and there is no general answer but that defined JAXBContextFactorys must exist in your classpath (wow what a wonder...) so you either have a that ClassNotFound Error because youre missing resources (well thats the acctual cause) or because you have a wrong JAXBContextFactory defined in any of the above mentioned propertie files which contain a definition according to the below answer.

非常感谢您的宝贵意见和支持,我真的很感激!

Very many thanks for your great comments and support, i realy appreciate!

推荐答案

您可以在与域模型相同的包中包含一个 jaxb.properties 文件来指定 JAXB (JSR-222) 您希望使用的实现.例如,它看起来像下面这样指定 EclipseLink MOXy作为您的 JAXB 提供商.

You can include a jaxb.properties file in the same package as your domain model to specify the JAXB (JSR-222) implementation you wish to use. For example it would look like the following to specify EclipseLink MOXy as your JAXB provider.

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

了解更多信息

这篇关于设置要使用的 JAXB 上下文工厂初始化类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆