Jackson Afterburner 模块在 Apache Karaf 3.0.5 中失败
Jackson Afterburner module is failing inside Apache Karaf 3.0.5
我在 Apache ServiceMix 6.1.0
中使用 Jackson's Afterburner Module 有一个简单的 Hello World 类型 Karaf Bundle 运行。激活代码如下所示:-
public class HelloWorldActivator implements BundleActivator {
@Override
public void start(BundleContext bundleContext) throws Exception {
System.out.println("STARTING DEMO: hello, world\n");
System.out.println(getJsonDataAsString());
}
@Override
public void stop(BundleContext bundleContext) throws Exception {
System.out.println("STOPPING DEMO");
}
private String getJsonDataAsString() {
JsonDataBlob jsonDataBlob = new JsonDataBlob();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new AfterburnerModule());
try {
return objectMapper.writeValueAsString(jsonDataBlob);
} catch(Exception e) {
e.printStackTrace();
}
return "";
}
}
pom.xml
如下所示:-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hello</groupId>
<artifactId>world</artifactId>
<version>0.0.1</version>
<packaging>bundle</packaging>
<name>Hello World</name>
<dependencies>
<!-- OSGi -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-afterburner</artifactId>
<version>2.7.1</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>**/com/hello/main/*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<inherited>true</inherited>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.hello.world.HelloWorldActivator</Bundle-Activator>
<Import-Package>*;resolution:=optional</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
我试图转换为 json 的 java 对象是一个简单的对象,如下所示:-
package com.hello.world;
public class JsonDataBlob {
private String add1 = "JP Naagar";
private String add2 = "";
private int shippartagent = 1;
public String getAdd1() {
return add1;
}
public void setAdd1(String add1) {
this.add1 = add1;
}
public String getAdd2() {
return add2;
}
public void setAdd2(String add2) {
this.add2 = add2;
}
public int getShippartagent() {
return shippartagent;
}
public void setShippartagent(int shippartagent) {
this.shippartagent = shippartagent;
}
}
但是每当我尝试安装捆绑包时,我都会收到以下异常并且捆绑包卡在 Resolved
状态:-
2016-05-07 15:36:48,986 | WARN | x-6.1-2.0/deploy | fileinstall | 7 - org.apache.felix.fileinstall - 3.5.0 | Error while starting bundle: file:/Users/debraj/Downloads/apache-servicemix-6.1-2.0/deploy/world-0.0.1.jar
org.osgi.framework.BundleException: Activator start error in bundle world [239].
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2196)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1245)[7:org.apache.felix.fileinstall:3.5.0]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1217)[7:org.apache.felix.fileinstall:3.5.0]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess(DirectoryWatcher.java:509)[7:org.apache.felix.fileinstall:3.5.0]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:358)[7:org.apache.felix.fileinstall:3.5.0]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:310)[7:org.apache.felix.fileinstall:3.5.0]
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/afterburner/AfterburnerModule
at java.lang.Class.getDeclaredConstructors0(Native Method)[:1.8.0_77]
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)[:1.8.0_77]
at java.lang.Class.getConstructor0(Class.java:3075)[:1.8.0_77]
at java.lang.Class.newInstance(Class.java:412)[:1.8.0_77]
at org.apache.felix.framework.Felix.createBundleActivator(Felix.java:4336)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2141)
... 7 more
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.module.afterburner.AfterburnerModule not found by world [239]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleWiringImpl.access0(BundleWiringImpl.java:75)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)[:1.8.0_77]
... 13 more
Karaf 中安装了所有必需的 Jackson 包:-
karaf@root>bundle:list | grep Jackson
123 | Active | 50 | 2.6.2 | Jackson-core
125 | Active | 50 | 2.6.2 | Jackson-annotations
238 | Installed | 80 | 2.7.1 | Jackson-module-Afterburner
karaf@root>bundle:list | grep jackson
124 | Active | 50 | 2.6.2 | jackson-databind
如果我注释掉以下行,一切正常:-
objectMapper.registerModule(new AfterburnerModule());
谁能告诉我我做错了什么?
所有代码我都放在了github.
您已经在 karaf 邮件列表中询问过这个问题。
它仍然是同一件事,只要包没有导入到您自己的应用程序中,它就不会工作。
由于您为 *;optional=true 进行了导入,因此您不会获得接线异常,因为您的导入都是可选的。因此 ClassNotFound 异常。
首先用
检查 headers 是否正确导入
bundle:header [bundle-id]
其次,由于您正在执行 * 导入,因此可能会发生这种情况,因为您没有明确要求某个包中的某个 class 某些包未导入。
sub-package 导入对包没有帮助,所以如果你有
的导入
com.fasterxml.jackson.module.afterburner.subPackage
这无助于解决
中的 classes
com.fasterxml.jackson.module.afterburner
所以实际上最好声明所有导入并仅在 sub-package 上使用 *。
在您的情况下,将以下内容添加到您的 pom:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<inherited>true</inherited>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.hello.world.HelloWorldActivator</Bundle-Activator>
<Import-Package>
com.fasterxml.jackson.module.afterburner.*,
*;resolution:=optional
</Import-Package>
</instructions>
</configuration>
</plugin>
同时升级到 Jackson 2.7.4。如下所示修改 pom.xml
中的导入包解决了问题:-
<Import-Package>com.fasterxml.jackson.module.afterburner.ser;resolution:=optional,*</Import-Package>
我在 Apache ServiceMix 6.1.0
中使用 Jackson's Afterburner Module 有一个简单的 Hello World 类型 Karaf Bundle 运行。激活代码如下所示:-
public class HelloWorldActivator implements BundleActivator {
@Override
public void start(BundleContext bundleContext) throws Exception {
System.out.println("STARTING DEMO: hello, world\n");
System.out.println(getJsonDataAsString());
}
@Override
public void stop(BundleContext bundleContext) throws Exception {
System.out.println("STOPPING DEMO");
}
private String getJsonDataAsString() {
JsonDataBlob jsonDataBlob = new JsonDataBlob();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new AfterburnerModule());
try {
return objectMapper.writeValueAsString(jsonDataBlob);
} catch(Exception e) {
e.printStackTrace();
}
return "";
}
}
pom.xml
如下所示:-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hello</groupId>
<artifactId>world</artifactId>
<version>0.0.1</version>
<packaging>bundle</packaging>
<name>Hello World</name>
<dependencies>
<!-- OSGi -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-afterburner</artifactId>
<version>2.7.1</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>**/com/hello/main/*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<inherited>true</inherited>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.hello.world.HelloWorldActivator</Bundle-Activator>
<Import-Package>*;resolution:=optional</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
</project>
我试图转换为 json 的 java 对象是一个简单的对象,如下所示:-
package com.hello.world;
public class JsonDataBlob {
private String add1 = "JP Naagar";
private String add2 = "";
private int shippartagent = 1;
public String getAdd1() {
return add1;
}
public void setAdd1(String add1) {
this.add1 = add1;
}
public String getAdd2() {
return add2;
}
public void setAdd2(String add2) {
this.add2 = add2;
}
public int getShippartagent() {
return shippartagent;
}
public void setShippartagent(int shippartagent) {
this.shippartagent = shippartagent;
}
}
但是每当我尝试安装捆绑包时,我都会收到以下异常并且捆绑包卡在 Resolved
状态:-
2016-05-07 15:36:48,986 | WARN | x-6.1-2.0/deploy | fileinstall | 7 - org.apache.felix.fileinstall - 3.5.0 | Error while starting bundle: file:/Users/debraj/Downloads/apache-servicemix-6.1-2.0/deploy/world-0.0.1.jar
org.osgi.framework.BundleException: Activator start error in bundle world [239].
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2196)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:955)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1245)[7:org.apache.felix.fileinstall:3.5.0]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1217)[7:org.apache.felix.fileinstall:3.5.0]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.doProcess(DirectoryWatcher.java:509)[7:org.apache.felix.fileinstall:3.5.0]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:358)[7:org.apache.felix.fileinstall:3.5.0]
at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:310)[7:org.apache.felix.fileinstall:3.5.0]
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/module/afterburner/AfterburnerModule
at java.lang.Class.getDeclaredConstructors0(Native Method)[:1.8.0_77]
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)[:1.8.0_77]
at java.lang.Class.getConstructor0(Class.java:3075)[:1.8.0_77]
at java.lang.Class.newInstance(Class.java:412)[:1.8.0_77]
at org.apache.felix.framework.Felix.createBundleActivator(Felix.java:4336)
at org.apache.felix.framework.Felix.activateBundle(Felix.java:2141)
... 7 more
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.module.afterburner.AfterburnerModule not found by world [239]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleWiringImpl.access0(BundleWiringImpl.java:75)[org.apache.felix.framework-4.2.1.jar:]
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)[:1.8.0_77]
... 13 more
Karaf 中安装了所有必需的 Jackson 包:-
karaf@root>bundle:list | grep Jackson
123 | Active | 50 | 2.6.2 | Jackson-core
125 | Active | 50 | 2.6.2 | Jackson-annotations
238 | Installed | 80 | 2.7.1 | Jackson-module-Afterburner
karaf@root>bundle:list | grep jackson
124 | Active | 50 | 2.6.2 | jackson-databind
如果我注释掉以下行,一切正常:-
objectMapper.registerModule(new AfterburnerModule());
谁能告诉我我做错了什么?
所有代码我都放在了github.
您已经在 karaf 邮件列表中询问过这个问题。 它仍然是同一件事,只要包没有导入到您自己的应用程序中,它就不会工作。 由于您为 *;optional=true 进行了导入,因此您不会获得接线异常,因为您的导入都是可选的。因此 ClassNotFound 异常。 首先用
检查 headers 是否正确导入bundle:header [bundle-id]
其次,由于您正在执行 * 导入,因此可能会发生这种情况,因为您没有明确要求某个包中的某个 class 某些包未导入。 sub-package 导入对包没有帮助,所以如果你有
的导入com.fasterxml.jackson.module.afterburner.subPackage
这无助于解决
中的 classescom.fasterxml.jackson.module.afterburner
所以实际上最好声明所有导入并仅在 sub-package 上使用 *。
在您的情况下,将以下内容添加到您的 pom:
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.4.0</version>
<inherited>true</inherited>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Bundle-Activator>com.hello.world.HelloWorldActivator</Bundle-Activator>
<Import-Package>
com.fasterxml.jackson.module.afterburner.*,
*;resolution:=optional
</Import-Package>
</instructions>
</configuration>
</plugin>
同时升级到 Jackson 2.7.4。如下所示修改 pom.xml
中的导入包解决了问题:-
<Import-Package>com.fasterxml.jackson.module.afterburner.ser;resolution:=optional,*</Import-Package>