7

我正在使用 Oracle Java 7 Update 3 在 Ubuntu 上运行测试。根据发行说明,它包含 JAXB-2.2.4。

/home/ubuntu# update-alternatives --config java
There is only one alternative in link group java: /usr/lib/jvm/java-7-oracle/bin/java
Nothing to configure.

机器说它上面有 JAXB-2.2.4:

$ wsimport -version
JAX-WS RI 2.2.4-b01

$ java -版本

java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b04)
Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode)

但是,当我运行使用 JAX-RS 并使用 JAXB 编组一些对象的应用程序时,出现以下错误:

Apr 05, 2012 10:45:50 AM com.sun.jersey.api.json.JSONConfiguration natural
SEVERE: NATURAL JSON notation configured, but JAXB RI 2.1.10 not found. For the
recent builds to get this working correctly, you need even at least JAXB version
 2.1.12. Please add it to your classpath!
Apr 05, 2012 10:45:50 AM com.sun.jersey.core.spi.component.ProviderFactory __get
ComponentProvider
SEVERE: The provider class, class com.marketchorus.rest.config.JAXBContextResolv
er, could not be instantiated. Processing will continue but the class will not b
e utilized
java.lang.RuntimeException: NATURAL JSON notation configured, but JAXB RI 2.1.10
 not found. For the recent builds to get this working correctly, you need even a
t least JAXB version 2.1.12. Please add it to your classpath!

在服务器端,我最初遇到了同样的错误,所以我将 metro-2.2-standalone 安装到了 tomcat 中,从而解决了这个问题。(即使它在 java 7 中运行)。

在 Windows 客户端上,我被迫将 JAXB-2.2.5 api、impl 和 jsr173 jar 添加到我认可的 java 目录中以使其工作(Java 7 update 3 也在那里)。

当我尝试在 ubuntu 下执行 jaxb 技巧时,我将文件放在 /usr/lib/jvm/java-7-oracle/jre/lib/endorsed 下,我得到一个不同的错误:

ERROR 10:55:44.694 taskScheduler-1 org.springframework.scheduling.support.TaskUt
ils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.MXParser
Factory not found

我正在我的班级中初始化 JAX-RS/JAXB 代码,如下所示:

config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
config.getClasses().add(JAXBContextResolver.class);
client = Client.create(config);
service = client.resource(UriBuilder.fromUri(proc.getUrl()).build());

我尝试使用 source=1.7 和 target=1.7 编译它,因为它仍然设置为 1.6,我在想它可能是假设早期 1.6 版本的旧版本太旧了,但这也不能解决问题。

我运行了@andih 的测试程序,但仍然出现错误。我开始认为这是一个虚拟机问题:

ubuntu@ip-10-202-155-29:~/test$ java -cp jersey-client-1.12.jar:jersey-core-1.1
2.jar:jersery-fastinfoset-1.12.jar:jersey-json-1.12.jar:. TestNaturalJson
Apr 23, 2012 10:17:21 AM com.sun.jersey.api.json.JSONConfiguration natural
SEVERE: NATURAL JSON notation configured, but JAXB RI 2.1.10 not found. For the
recent builds to get this working correctly, you need even at least JAXB version
 2.1.12. Please add it to your classpath!
Exception in thread "main" java.lang.RuntimeException: NATURAL JSON notation con
figured, but JAXB RI 2.1.10 not found. For the recent builds to get this working
 correctly, you need even at least JAXB version 2.1.12. Please add it to your cl
asspath!
        at com.sun.jersey.api.json.JSONConfiguration.natural(JSONConfiguration.j
ava:447)
        at TestNaturalJson.main(TestNaturalJson.java:6)
ubuntu@ip-10-202-155-29:~/test$ java -version
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b04)
Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode)

回顾 JAXB-2.2.5 RI 认可的解决方案,我安装了文件:

jaxb-api.jar
jaxb-impl.jar
jsr173_1.0_api.jar

进入:

/usr/lib/jvm/java-7-oracle/jre/lib/endorsed

目录。当我这样做时,我从 Spring MXParser 错误中得到上述错误。这里奇怪的是在 Windows 下可以正常工作,但在 Ubuntu 上不行。当我在下面使用认可的设置运行@andhi 的测试程序时,我得到了 OK 而不是错误。但是由于某种原因,Ubuntu下的Spring似乎没有选择安装在认可目录(即STAX实现)中的JSR173 jar文件。

4

3 回答 3

3

在我看来,这就像一个类路径问题。您是否检查过您的类路径中是否有其他jaxb-impl-x.y.z.jarjaxb-api-k.l.m.jars?JSonConfiguration查找com.sun.xml.bind.annotation.OverrideAnnotationOf属于 JRE7的类rt.jar

您可以使用以下简单程序检查您的JRE安装:

import com.sun.jersey.api.json.JSONConfiguration;


public class TestNaturalJSon {
    public static void main(String[] args) {
        JSONConfiguration config = JSONConfiguration.natural().build();

        System.out.println("OK");

    }

}

如果您使用 maven,则可以使用具有以下依赖项的 pom:

<dependencies>
  <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.12</version>

    <exclusions>
        <exclusion>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        </exclusion>
        <exclusion>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        </exclusion>
    </exclusions>

   </dependency>
</dependencies>
于 2012-04-21T05:01:11.270 回答
2

要知道从哪里加载类,您应该运行带有-verbose:class选项的 JVM 命令行。

这样,您可以通过在javax.xml.stream.FactoryConfigurationError抛出异常之前或就在诊断消息之前查看最新的详细行来猜测出了什么问题JAXB RI 2.1.10 not found

所以你可以得到一个实现来自的 jar 并检查该endorsed目录是否被使用......

于 2012-04-24T10:11:01.527 回答
1

好吧,我很欣赏这里的所有评论,但没有人能解决我的问题,所以我按如下方式解决了它。我将 Metro-2.2 安装到 Tomcat 7 中。然后由于我的应用程序是 Spring 命令行应用程序,因此我用它构建了一个战争,以便它可以在 tomcat 容器中运行。这涉及将我的应用程序的所有库放在 WEB-INF/lib 中,我的配置文件放在 WEB-INF/classes 中。在 WEB-INF 目录中,我放置了一个如下所示的 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/remote-scheduler-spring-config.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>

index.jsp 为空,因为此应用程序没有面向 Web 的组件。spring-config 参数是我将从命令行加载的顶级 spring 配置文件。然后我的 ant build 用这个应用程序构建了一场战争,它运行得很好。所以仍然不确定为什么我在 jvm 中认可的文件没有像建议的那样使用 verbose:class 标志显示它们正在加载,但是通过使用 tomcat 作为容器并在那里加载我需要的类,Jersey 很高兴并且所有问题已经消失。再次感谢您在此问题上的所有帮助。

于 2012-04-25T16:46:03.917 回答