我有Equinox,Jersey和MOXy的工作环境。 Jersey使用MOXy来解析JSON并绑定到bean对象。将某些@XmlRootElement类的字段类型更改为枚举后,我得到:
java.lang.NoClassDefFoundError: org/eclipse/persistence/internal/jaxb/WrappedValue
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.lang.ClassLoader.defineClass(ClassLoader.java:642)
at org.eclipse.persistence.internal.jaxb.JaxbClassLoader.generateClass(JaxbClassLoader.java:124)
at org.eclipse.persistence.jaxb.compiler.MappingsGenerator.generateWrapperClass(MappingsGenerator.java:3232)
...
MOXy捆绑似乎没问题:
osgi> lb -l
...
50|Active | 4|initial@reference:file:plugins/org.eclipse.persistence.moxy_2.6.0.v20150309-bf26070.jar
...
jar中有类
$ jar tf plugins/org.eclipse.persistence.moxy_2.6.0.v20150309-bf26070.jar | grep Wrapped
org/eclipse/persistence/internal/jaxb/WrappedValue.class
并导出包
osgi> bundle 50
...
Exported packages
org.eclipse.persistence.internal.jaxb; version="2.6.0"[exported]
...
那为什么我会得到ClassNotFoundException?
编辑: bundes:
0|Active | 0|System Bundle
1|Active | 4|initial@reference:file:plugins/org.mongodb.morphia_0.110.0.jar
2|Active | 4|initial@reference:file:plugins/javax.validation.api_1.1.0.Final.jar
3|Active | 4|initial@reference:file:plugins/classmate_1.0.0.jar
4|Active | 4|initial@reference:file:plugins/org.hibernate.validator_5.1.3.Final.jar
5|Active | 4|initial@reference:file:plugins/commons-logging_1.1.1.jar
6|Active | 4|initial@reference:file:plugins/org.eclipse.jetty.server_8.1.16.v20140903.jar
7|Active | 4|initial@reference:file:plugins/com.thoughtworks.proxytoys_1.0.0.jar
8|Active | 4|initial@reference:file:plugins/org.eclipse.jetty.http_8.1.16.v20140903.jar
9|Active | 4|initial@reference:file:plugins/org.eclipse.equinox.http.jetty_3.0.200.v20131021-1843.jar
10|Active | 4|initial@reference:file:../../../../edu.zut.aci.nsg.restapi/
11|Active | 4|initial@reference:file:plugins/org.mongodb.morphia.util_0.110.0.jar
12|Active | 4|initial@reference:file:plugins/org.eclipse.osgi.services_3.4.0.v20140312-2051.jar
13|Active | 4|initial@reference:file:plugins/org.eclipse.equinox.http.servlet_1.1.500.v20140318-1755.jar
14|Active | 4|initial@reference:file:plugins/org.glassfish.javax.json_1.0.4.jar
15|Active | 4|initial@reference:file:plugins/org.glassfish.jersey.ext.jersey-bean-validation_2.17.0.jar
16|Active | 4|initial@reference:file:plugins/org.glassfish.jersey.media.jersey-media-jaxb_2.17.0.jar
17|Active | 4|initial@reference:file:plugins/org.glassfish.hk2.utils_2.4.0.b10.jar
18|Active | 4|initial@reference:file:plugins/org.glassfish.jersey.containers.jersey-container-servlet_2.17.0.jar
19|Active | 4|initial@reference:file:plugins/org.glassfish.jersey.core.jersey-client_2.17.0.jar
20|Active | 4|initial@reference:file:plugins/org.eclipse.persistence.core_2.6.0.v20150309-bf26070.jar
21|Active | 4|initial@reference:file:plugins/javassist_3.18.1.GA.jar
22|Active | 4|initial@reference:file:plugins/org.eclipse.jetty.security_8.1.16.v20140903.jar
23|Active | 4|initial@reference:file:plugins/org.glassfish.hk2.external.javax.inject_2.4.0.b10.jar
24|Active | 4|initial@reference:file:plugins/com.eclipsesource.jaxrs.publisher_4.3.0.201503290743.jar
25|Active | 4|initial@reference:file:plugins/org.mongodb.mongo-java-driver_2.13.0.RELEASE.jar
26|Active | 4|initial@reference:file:plugins/org.eclipse.persistence.asm_5.0.1.v201405080102.jar
27|Active | 4|initial@reference:file:plugins/com.google.gson_2.3.1.jar
28|Active | 4|initial@reference:file:plugins/org.eclipse.jetty.util_8.1.16.v20140903.jar
29|Active | 4|initial@reference:file:plugins/org.eclipse.jetty.servlet_8.1.16.v20140903.jar
30|Active | 4|initial@reference:file:plugins/org.eclipse.jetty.io_8.1.16.v20140903.jar
31|Active | 1|initial@reference:file:plugins/org.eclipse.equinox.ds_1.4.200.v20131126-2331.jar
32|Active | 4|initial@reference:file:plugins/org.glassfish.hk2.api_2.4.0.b10.jar
33|Active | 4|initial@reference:file:plugins/org.apache.felix.gogo.runtime_0.10.0.v201209301036.jar
34|Active | 4|initial@reference:file:plugins/org.eclipse.jetty.continuation_8.1.16.v20140903.jar
35|Active | 4|initial@reference:file:plugins/org.eclipse.equinox.console_1.1.0.v20140131-1639.jar
36|Active | 4|initial@reference:file:plugins/com.eclipsesource.jaxrs.provider.security_2.1.0.201503290743.jar
37|Active | 4|initial@reference:file:plugins/javax.el_2.2.0.v201303151357.jar
38|Active | 4|initial@reference:file:plugins/javax.ws.rs-api_2.0.1.jar
39|Active | 4|initial@reference:file:plugins/org.glassfish.hk2.osgi-resource-locator_1.0.1.jar
40|Active | 4|initial@reference:file:plugins/org.glassfish.jersey.bundles.repackaged.jersey-guava_2.17.0.jar
41|Active | 4|initial@reference:file:plugins/org.jboss.logging.jboss-logging_3.1.3.GA.jar
42|Active | 4|initial@reference:file:plugins/org.eclipse.equinox.util_1.0.500.v20130404-1337.jar
43|Active | 4|initial@reference:file:plugins/org.glassfish.hk2.external.aopalliance-repackaged_2.4.0.b10.jar
44|Active | 4|initial@reference:file:plugins/javax.servlet-api_3.0.1.jar
45|Active | 4|initial@reference:file:plugins/org.apache.felix.gogo.shell_0.10.0.v201212101605.jar
46|Active | 4|initial@reference:file:plugins/org.glassfish.jersey.core.jersey-common_2.17.0.jar
47|Active | 4|initial@reference:file:plugins/org.glassfish.hk2.locator_2.4.0.b10.jar
48|Active | 4|initial@reference:file:plugins/javax.annotation-api_1.2.0.jar
49|Active | 4|initial@reference:file:plugins/org.apache.commons.configuration_1.10.0.jar
50|Active | 4|initial@reference:file:plugins/org.eclipse.persistence.moxy_2.6.0.v20150309-bf26070.jar
51|Active | 4|initial@reference:file:plugins/org.scala-lang.scala-library_2.11.6.v20150224-172222-092690e7bf.jar
52|Active | 4|initial@reference:file:plugins/org.apache.commons.lang_2.6.0.jar
53|Active | 4|initial@reference:file:plugins/org.glassfish.web.javax.el_2.2.4.jar
54|Active | 4|initial@reference:file:plugins/org.slf4j.api_1.7.2.v20121108-1250.jar
55|Active | 4|initial@reference:file:plugins/org.glassfish.jersey.containers.jersey-container-servlet-core_2.17.0.jar
56|Active | 4|initial@reference:file:plugins/org.apache.felix.gogo.command_0.10.0.v201209301215.jar
57|Active | 4|initial@reference:file:plugins/org.glassfish.jersey.core.jersey-server_2.17.0.jar
导致问题的数据模型的变化:
从:
@XmlRootElement
public class Role implements Serializable {
public static final String COMMON = "common";
public static final String REGISTRANT = "registrant";
...
为:
@XmlEnum(String.class)
public enum Role {
@XmlEnumValue("common") COMMON ("common"),
@XmlEnumValue("registrant") REGISTRANT ("registrant"),
@XmlEnumValue("laborant") LABORANT ("laborant"),
...
用法如下:
@XmlRootElement
public class User implements Principal, Serializable {
private static final long serialVersionUID = 1L;
...
private List<Role> roles = new ArrayList<Role>();
...
和
@PUT @Path("/{name}")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
public Response register(@PathParam("name") String name, @Valid User user) {
...
答案 0 :(得分:1)
我相信它可能是由于使用JAXB的bundle中的Import-Package清单头中缺少条目引起的。我有一个类似的堆栈跟踪,当我添加org.eclipse.persistence.jaxb
时,它有效。
比照。 Maven Bundle插件的以下片段:
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<instructions>
<Import-Package>
org.eclipse.persistence.jaxb,
*
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
答案 1 :(得分:1)
您的OSGi清单文件中缺少导入包。只将此行添加到清单文件中,这位于MANIFEST.MF
类所在的包中的Role
中:
DynamicImport-Package: org.eclipse.persistence.internal.*,org.eclipse.persistence.internal.jaxb.*
顺便说一下,你不需要@XmlEnum(String.class)
类之上的注释Role
,把它留作简单的POJO,它会起作用。
答案 2 :(得分:0)
除了@ChristofferSoop答案。
如果您在日食环境中工作,则只需将MOXy捆绑包添加到依赖项列表中即可。