序列化java.lang.Error

时间:2016-08-17 11:02:52

标签: java serialization jackson resteasy

我正在序列化以下类,因此我可以通过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。

1 个答案:

答案 0 :(得分:0)

UnrecognizedPropertyException - 专门用于指示由于遇到无法映射到Object属性的JSON属性(通过getter,构造函数参数或字段)导致的问题的专用JsonMappingException子类。 (来源:https://fasterxml.github.io/jackson-databind/javadoc/2.3.0/com/fasterxml/jackson/databind/exc/UnrecognizedPropertyException.html

你能展示所有的二传手和吸气鬼吗?