使用Jackson ObjectMapper进行序列化时的ClassCastException - java.lang.object作为LinkedHashset中使用的底层HashMap中的值

时间:2013-11-13 01:17:48

标签: java serialization jackson classcastexception linkedhashset

是的,标题很混乱,我知道!

我正在使用Jackson ObjectMapper来序列化MyClass类型的LnkedHashSet。这就是将东西添加到该HashSet

的方式
private LinkedHashSet<MyClass> causeEntities;
this.causeEntities.add(new MyClass("foo"));

现在,当Jackson尝试序列化时,它会抛出一个ClassCastException,说java.lang.Object不能转换为MyClass。我调试了LinkedHashSet中的“添加”并将其添加到Map中,其中键为MyClass,值为虚拟对象

private transient HashMap<E,Object> map;
private static final Object PRESENT = new Object();
public boolean add(E e) {
    return map.put(e, PRESENT)==null;
}

因此,Jackson获取的最终LinkedHashSet包含一个HashMap,而HashMap又是一个MyClass-Object的键值对,而这个Object给了我一个噩梦。

如何解决这个问题的任何想法都将受到高度赞赏。我是新手,如果有人可以提供帮助,我会很高兴,谢谢!

2013/11/12 17:32:30,058 SEVERE [com.servicebridge.service] : org.codehaus.jackson.map.JsonMappingException: [Ljava.lang.Object; cannot be cast to [Lcom.MyClass; (through reference chain: com.blah.ABC["details"]->com.blah.DEF["validationAppErrors"]->com.blah.GHI["causeEntity"])
    at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.std.SerializerBase.wrapAndThrow(SerializerBase.java:140) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:158) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.std.ObjectArraySerializer.serializeContents(ObjectArraySerializer.java:123) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.std.ObjectArraySerializer.serializeContents(ObjectArraySerializer.java:29) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.std.StdArraySerializers$ArraySerializerBase.serialize(StdArraySerializers.java:56) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:2568) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ObjectMapper.writeValueAsString(ObjectMapper.java:2090) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at com.blah.JKL.invokeService(JKL.java:106) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_11]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_11]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_11]
    at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_11]
    at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:180) [cxf-api-2.7.5.jar:2.7.5]
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96) [cxf-api-2.7.5.jar:2.7.5]
    at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:198) [cxf-bundle-jaxrs-2.7.5.jar:2.7.5]
    at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:100) [cxf-bundle-jaxrs-2.7.5.jar:2.7.5]
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58) [cxf-api-2.7.5.jar:2.7.5]
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:94) [cxf-api-2.7.5.jar:2.7.5]
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271) [cxf-api-2.7.5.jar:2.7.5]
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) [cxf-api-2.7.5.jar:2.7.5]
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239) [cxf-bundle-jaxrs-2.7.5.jar:2.7.5]
    at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223) [cxf-bundle-jaxrs-2.7.5.jar:2.7.5]
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203) [cxf-bundle-jaxrs-2.7.5.jar:2.7.5]
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137) [cxf-bundle-jaxrs-2.7.5.jar:2.7.5]
    at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158) [cxf-bundle-jaxrs-2.7.5.jar:2.7.5]
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:243) [cxf-bundle-jaxrs-2.7.5.jar:2.7.5]
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:163) [cxf-bundle-jaxrs-2.7.5.jar:2.7.5]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-servlet-api_3.0_spec-1.0.1.Final.jar:1.0.1.Final]
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:219) [cxf-bundle-jaxrs-2.7.5.jar:2.7.5]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.17.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.17.Final.jar:]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:366) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:99) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at com.blah.MNO.doFilter(MNO.java:93) [foo-1.0.jar:]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at com.blah.PQR.doFilter(PQR.java:113) [foo-1.0.jar:]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:167) [spring-security-web-3.0.4.RELEASE.jar:3.0.4.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167) [spring-web-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.17.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.17.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.17.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.17.Final.jar:]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:165) [jboss-as-web-7.1.3.Final.jar:7.1.3.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.17.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.17.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.17.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:372) [jbossweb-7.0.17.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.17.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:679) [jbossweb-7.0.17.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:931) [jbossweb-7.0.17.Final.jar:]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_11]
Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.MyClass;
    at com.blah.GHI.getCauseEntity(GHI.java:80) [web-sim-1.0.jar:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_11]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_11]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_11]
    at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_11]
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:483) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:418) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) [jackson-mapper-asl-1.9.3.jar:1.9.3]
    ... 75 more

2 个答案:

答案 0 :(得分:2)

Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.MyClass;
    at com.blah.GHI.getCauseEntity(GHI.java:80) [web-sim-1.0.jar:]

我会在这里冒一些水晶球调试:根据堆栈跟踪,错误与序列化本身无关,而是试图将Object[]强制转换为MyClass[] } GHI.java

您是否有机会在没有参数的泛型集合上使用.toArray()?这将返回Object[],不能进行缩小演员。在这种情况下,请使用one-argument overload.toArray(new MyClass[0]),以确保创建的数组的类型正确。

如果您无法发布实际代码并且不得不采用GHIJKLMyClass之类的混淆,那么这会使问题排查变得更加困难。

答案 1 :(得分:0)

修复我的问题的代码:

<强>之前:

return (MyClass[]) ((this.causeEntities == null ) ? null : this.causeEntities.toArray());

<强>后:

return (MyClass[]) ((this.causeEntities == null ) ? null : this.causeEntities.toArray(new MyClass[this.causeEntities.size()]));