【发布时间】:2016-08-17 11:02:51
【问题描述】:
我正在序列化以下类,以便可以通过 REST 接口发送:
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
/**
* Object that holds information on result of executed method on remote agent.
*/
public class ResultMessage implements Message {
private static final long serialVersionUID = -8740453631197286688L;
private Exception exception;
private Error error;
private String exceptionType;
private String errorType;
public String getCmdResponse() {
return cmdResponse;
}
public Exception getException() {
return exception;
}
public void setException(Exception e) {
this.exception = e;
if (e != null) {
this.exceptionType = e.getClass().getName();
}
}
public String getExceptionType() {
return exceptionType;
}
public Error getError() {
return error;
}
public void setError(Error error) {
this.error = error;
if (error != null) {
this.errorType = error.getClass().getName();
}
}
public String getErrorType() {
return errorType;
}
}
我可以毫无问题地对异常进行编码,但我似乎无法对错误进行编码。以下错误:
java.lang.NoClassDefFoundError: com/cfg/ObjectRepositoryFactory
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.cfg.ObjectRepositoryFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 7 more
当我尝试序列化时,我得到:
org.jboss.resteasy.spi.ReaderException:
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized
field "exception" (Class java.lang.Throwable), not marked as ignorable
at [Source: org.jboss.netty.buffer.ChannelBufferInputStream@6cc1e35f;
line: 1, column: 2080] (through reference chain:
com.rest.message.ResultMessage["error"]->java.lang.Throwable["exception"])
这是设置字段的地方:
try {
verb.runCommand(result);
} catch (Exception e) {
LOGGER.fatal("Failed to execute command [" + msg.getCommand() + "] due to exception");
System.err.println("Exception: ");
e.printStackTrace();
LOGGER.trace(e.getMessage(), e);
result.setException(e);
} catch (Error ee) {
result.setError(ee);
System.err.println("Error: ");
ee.printStackTrace();
LOGGER.trace(ee.getMessage(), ee);
LOGGER.fatal("Failed to execute command [" + msg.getCommand() + "] due to error");
} finally {
sendResult(result);
}
当我捕获 java.lang.Exception 时,它可以正常工作,但不是这个特定的 java.lang.Error。
【问题讨论】:
-
您真的需要使用堆栈跟踪和所有内容来跟踪和序列化完整的 Throwables 吗?你能不能只在错误发生时记录错误并跟踪错误代码或-message?
标签: java serialization jackson resteasy