JAXB在Java 7下发布,它说它无法找到RI 2.1.0

has*_*vec 7 java linux jax-rs jaxb jersey

我正在使用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.
Run Code Online (Sandbox Code Playgroud)

机器上说它上面有JAXB-2.2.4:

$ wsimport -version
JAX-WS RI 2.2.4-b01
Run Code Online (Sandbox Code Playgroud)

$ 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)
Run Code Online (Sandbox Code Playgroud)

但是,当我运行使用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!
Run Code Online (Sandbox Code Playgroud)

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

在Windows客户端上,我被迫将JAXB-2.2.5 api,impl和jsr173 jar添加到我认可的java目录中以使其工作(在那里也使用Java 7更新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
Run Code Online (Sandbox Code Playgroud)

我正在我的类中初始化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());
Run Code Online (Sandbox Code Playgroud)

我尝试用source = 1.7和target = 1.7编译它,因为它仍然设置为1.6,我想也许它假设早期的1.6版本中的旧版本太旧了但是也没有解决问题.

我跑了@ andih的测试程序,我仍然得到错误.我开始认为这是一个VM问题:

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)
Run Code Online (Sandbox Code Playgroud)

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

jaxb-api.jar
jaxb-impl.jar
jsr173_1.0_api.jar
Run Code Online (Sandbox Code Playgroud)

进入:

/usr/lib/jvm/java-7-oracle/jre/lib/endorsed
Run Code Online (Sandbox Code Playgroud)

目录.当我这样做时,我从Spring得到了上述错误MXParser错误.这里的奇怪的事情是在Windows下工作,但不是在Ubuntu上.当我使用认可的设置运行下面的@ andhi的测试程序时,我得到了OK而不是错误.但由于某种原因,看起来像Ubuntu下的Spring没有拿起安装在背书目录(这是STAX实现)中的JSR173 jar文件.

has*_*vec 1

好吧,我很感谢这里的所有评论,但没有人能够解决我的问题,所以我按如下方式解决了这个问题。我将 Metro-2.2 安装到 Tomcat 7 中。然后,由于我的应用程序是 Spring 命令行应用程序,我用它构建了一个 war,以便它可以在 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>
Run Code Online (Sandbox Code Playgroud)

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