【问题标题】:Serializing java.lang.Error序列化 java.lang.Error
【发布时间】: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


【解决方案1】:

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

你能展示所有的 setter 和 getter 吗?

【讨论】:

  • 抱歉,我忘记了错误的设置器/获取器 - 我已经添加了这些。
  • 我相信这与提到的stackoverflow.com/questions/17855538/… 完全相同。 Jackson 对 Throwable 有问题,如何解决它也写在那里。
猜你喜欢
  • 1970-01-01
  • 2010-09-26
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
  • 1970-01-01
  • 2011-08-10
  • 2012-03-17
相关资源
最近更新 更多