我正在使用 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文件。