您好,我在嘗試將對象轉換爲JSON字符串時遇到以下異常。我不確定爲何顯示此錯誤?Java異常com/fasterxml/jackson/core/util/Instantiatable
String json = "";
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
try
{
// Convert our object to a string
json = ow.writeValueAsString(obj); <----Exception throwing code
}
catch (JsonProcessingException e)
{
ConsoleLog.DisplayToConsole("Exception: parse JSON: " + e.getMessage());
}
我已經加入了傑克遜庫的:
public class AccData extends AbstractData
{
private static ArrayList<Float> Acc_For = new ArrayList<Float>();
private static ArrayList<Float> Acc_Side = new ArrayList<Float>();
private static ArrayList<Float> Acc_Up = new ArrayList<Float>();
public static ArrayList<Float> getAccFor()
{
return Acc_For;
}
public static ArrayList<Float> getAcSide()
{
return Acc_Side;
}
etc
堆棧跟蹤:我想嘗試轉換到JSON
對象定義
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/util/Instantiatable
at com.fasterxml.jackson.databind.ObjectWriter._configureJsonGenerator(ObjectWriter.java:729)
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:601)
at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsString(ObjectWriter.java:530)
at controller.JSONController.ObjectToJSON(JSONController.java:35)
at controller.MongoDBController.generateJSONStrings(MongoDBController.java:64)
at view.ChartWindow$1.widgetSelected(ChartWindow.java:164)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759)
at view.ChartWindow.main(ChartWindow.java:186)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.util.Instantiatable
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
我刪除了jackson-annotations.jar文件夾並添加了更新的版本。但現在我得到這個錯誤:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonAutoDetect
這裏時拋出(如上):
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
堆棧跟蹤:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonAutoDetect
at com.fasterxml.jackson.databind.introspect.VisibilityChecker$Std.<clinit>(VisibilityChecker.java:172)
at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:195)
at controller.JSONController.ObjectToJSON(JSONController.java:32)
at controller.MongoDBController.generateJSONStrings(MongoDBController.java:64)
at view.ChartWindow$1.widgetSelected(ChartWindow.java:164)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759)
at view.ChartWindow.main(ChartWindow.java:186)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonAutoDetect
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 11 more
您尚未發佈堆棧跟蹤。請發佈完整的堆棧跟蹤(包含所有「引起來的」部分),並在代碼中標識引發異常的行。 –
你應該使用像maven這樣的真正的依賴關係管理器,而不是手動創建一個「lib」文件夾 –
查看jackson-databind的依賴關係,它依賴於jackson-annotations以及jackson-core http://search.maven .ORG /#artifactdetails%7Ccom.fasterxml.jackson.core%7Cjackson-數據綁定%7C2.7.3%7Cbundle –