使Jackson ObjectMapper可序列化

时间:2012-03-05 13:03:16

标签: serialization wicket jackson

我正试图在Wicket中使用Jackson的ObjectMapper。它一般都在工作,但抛出序列化异常:[class=org.codehaus.jackson.map.ObjectMapper] <----- field that is not serializable

我正在使用private ObjectMapper mapper = new ObjectMapper();设置字段。

尝试private ObjectMapper mapper = new ObjectMapper().setSerializerProvider(new StdSerializerProvider());会导致相同的结果。我假设我错过了正确的(组合)配置?!任何指向正确组合的指针都将受到高度赞赏。

完整堆栈跟踪:

*ERROR:Error serializing object class ....web.backend.overview.OverviewPage [object=[Page class = ....web.backend.overview.OverviewPage, id = 1, render count = 1]]
org.apache.wicket.util.io.SerializableChecker$WicketNotSerializableException: Unable to serialize class: org.codehaus.jackson.map.ObjectMapper
Field hierarchy is:
  1 [class=....web.backend.overview.OverviewPage, path=1]
    private java.lang.Object org.apache.wicket.MarkupContainer.children [class=[Ljava.lang.Object;]
      private org.apache.velocity.app.VelocityEngine ....web.BasePanel.velocityEngine[5] [class=....web.backend.overview.MailBoxQuickOverViewPanel, path=1:mailboxQuickOverviewPanel]
        private java.lang.Object org.apache.wicket.MarkupContainer.children [class=[Ljava.lang.Object;]
          private java.lang.Object org.apache.wicket.MarkupContainer.children[0] [class=....web.backend.overview.ChartPanel, path=1:mailboxQuickOverviewPanel:chartPanel]
            private java.lang.Object org.apache.wicket.MarkupContainer.children [class=com.comsysto.insight.component.HighchartsPanel, path=1:mailboxQuickOverviewPanel:chartPanel:chart]
              private java.lang.Object org.apache.wicket.MarkupContainer.children [class=[Ljava.lang.Object;]
                private java.lang.Object org.apache.wicket.MarkupContainer.children[1] [class=org.apache.wicket.markup.html.basic.Label, path=1:mailboxQuickOverviewPanel:chartPanel:chart:script]
                  java.lang.Object org.apache.wicket.Component.data [class=com.comsysto.insight.component.HighchartsPanel$1]
                    final com.comsysto.insight.model.Highchart com.comsysto.insight.component.HighchartsPanel$1.val$highcharts [class=com.comsysto.insight.model.Highchart]
                      private org.codehaus.jackson.map.ObjectMapper com.comsysto.insight.model.Highchart.mapper [class=org.codehaus.jackson.map.ObjectMapper] <----- field that is not serializable
    at org.apache.wicket.util.io.SerializableChecker.internalCheck(SerializableChecker.java:386) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:365) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.checkFields(SerializableChecker.java:646) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.internalCheck(SerializableChecker.java:569) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:365) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.checkFields(SerializableChecker.java:646) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.internalCheck(SerializableChecker.java:569) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:365) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.checkFields(SerializableChecker.java:646) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.internalCheck(SerializableChecker.java:569) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:365) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.internalCheck(SerializableChecker.java:431) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:365) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.checkFields(SerializableChecker.java:646) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.internalCheck(SerializableChecker.java:569) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:365) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.checkFields(SerializableChecker.java:646) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.internalCheck(SerializableChecker.java:569) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:365) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.internalCheck(SerializableChecker.java:431) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:365) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.checkFields(SerializableChecker.java:646) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.internalCheck(SerializableChecker.java:569) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:365) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.internalCheck(SerializableChecker.java:431) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:365) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.checkFields(SerializableChecker.java:646) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.internalCheck(SerializableChecker.java:569) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.check(SerializableChecker.java:365) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.io.SerializableChecker.writeObjectOverride(SerializableChecker.java:715) ~[wicket-core-1.5.4.jar:1.5.4]
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326) ~[na:1.6.0_29]
    at org.apache.wicket.serialize.java.JavaSerializer$CheckerObjectOutputStream.writeObjectOverride(JavaSerializer.java:258) ~[wicket-core-1.5.4.jar:1.5.4]
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326) ~[na:1.6.0_29]
    at org.apache.wicket.serialize.java.JavaSerializer.serialize(JavaSerializer.java:77) ~[wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.pageStore.DefaultPageStore.serializePage(DefaultPageStore.java:368) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.pageStore.DefaultPageStore.storePage(DefaultPageStore.java:146) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.page.PageStoreManager$PersistentRequestAdapter.storeTouchedPages(PageStoreManager.java:383) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.page.RequestAdapter.commitRequest(RequestAdapter.java:171) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.page.AbstractPageManager.commitRequest(AbstractPageManager.java:94) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.page.PageManagerDecorator.commitRequest(PageManagerDecorator.java:68) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.page.PageAccessSynchronizer$2.commitRequest(PageAccessSynchronizer.java:281) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.Application$2.onDetach(Application.java:1598) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.request.cycle.RequestCycleListenerCollection$3.notify(RequestCycleListenerCollection.java:99) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.request.cycle.RequestCycleListenerCollection$3.notify(RequestCycleListenerCollection.java:97) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.listener.ListenerCollection$1.notify(ListenerCollection.java:119) [wicket-util-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.listener.ListenerCollection.reversedNotify(ListenerCollection.java:143) [wicket-util-1.5.4.jar:1.5.4]
    at org.apache.wicket.util.listener.ListenerCollection.reversedNotifyIgnoringExceptions(ListenerCollection.java:113) [wicket-util-1.5.4.jar:1.5.4]
    at org.apache.wicket.request.cycle.RequestCycleListenerCollection.onDetach(RequestCycleListenerCollection.java:95) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.request.cycle.RequestCycle.onDetach(RequestCycle.java:600) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.request.cycle.RequestCycle.detach(RequestCycle.java:539) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:287) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:162) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:218) [wicket-core-1.5.4.jar:1.5.4]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.25]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.25]
    at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:359) [shiro-web-1.1.0.jar:na]
    at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:275) [shiro-web-1.1.0.jar:na]
    at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) [shiro-core-1.1.0.jar:1.1.0]
    at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) [shiro-core-1.1.0.jar:1.1.0]
    at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:344) [shiro-core-1.1.0.jar:1.1.0]
    at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:272) [shiro-web-1.1.0.jar:na]
    at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:81) [shiro-web-1.1.0.jar:na]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) [spring-web-3.1.0.RELEASE.jar:3.1.0.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [catalina.jar:7.0.25]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [catalina.jar:7.0.25]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224) [catalina.jar:7.0.25]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) [catalina.jar:7.0.25]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) [catalina.jar:7.0.25]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) [catalina.jar:7.0.25]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) [catalina.jar:7.0.25]
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) [catalina.jar:7.0.25]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [catalina.jar:7.0.25]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) [catalina.jar:7.0.25]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987) [tomcat-coyote.jar:7.0.25]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579) [tomcat-coyote.jar:7.0.25]
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309) [tomcat-coyote.jar:7.0.25]
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_29]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_29]
    at java.lang.Thread.run(Thread.java:680) [na:1.6.0_29]
Caused by: java.io.NotSerializableException: org.codehaus.jackson.map.ObjectMapper
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1164) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1346) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1154) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1518) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1483) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1400) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158) ~[na:1.6.0_29]
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:330) ~[na:1.6.0_29]
    at org.apache.wicket.serialize.java.JavaSerializer$CheckerObjectOutputStream.writeObjectOverride(JavaSerializer.java:250) ~[wicket-core-1.5.4.jar:1.5.4]
    ... 48 common frames omitted

1 个答案:

答案 0 :(得分:3)

ObjectMapper不应序列化。它不是可序列化的,并且有充分的理由。

因此,您还有其他一些与您的使用相关的问题:正如其他人指出的那样,ObjectMapper的可序列化或缺乏可能导致问题的原因。 很可能你不小心有依赖Mapper等:删除那些依赖。映射器通常作为静态单例(很好)访问,或者使用Guice或Spring IoC注入。

基于原始问题,您可能应该只创建ObjectMapper的静态实例 - 非常重要的是不要为每个请求创建新实例,而是重用实例。

编辑(2013/10):正如@Jason所指出的,ObjectMapper,这已不再适用。事实证明,有一个潜在的原因,当在某些平台上使用时 - 例如,在Android上,“冻结”映射器的能力可以获得性能。我不建议将其作为默认策略,但至少是一个选项。