Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson 1.8.5 (for JSON) does not deserialize: "no such class found"

I'm using JSON for my RESTful services and I have JSON (as the payload carrier format).

I'm using @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "class") on interface IntA. Class SuperClass implements IntA. MyClass extends SuperClass

The problem I have is little weird. When I serialize and deserialize my classes using standalone main program, it works perfectly fine. However, when I deploy it over tomcat in a war and I try to deserialize, it says no such class found

Stacktrace:

Caused by: java.lang.IllegalArgumentException: Invalid type id 'com.abc.xyz.MyClass' (for id type 'Id.class'): no such class found
        at org.codehaus.jackson.map.jsontype.impl.ClassNameIdResolver.typeFromId(ClassNameIdResolver.java:55)
        at org.codehaus.jackson.map.jsontype.impl.TypeDeserializerBase._findDeserializer(TypeDeserializerBase.java:77)
        at org.codehaus.jackson.map.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:67)
        at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeWithType(BeanDeserializer.java:423)
        at org.codehaus.jackson.map.deser.StdDeserializerProvider$WrappedDeserializer.deserialize(StdDeserializerProvider.java:460)
        at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2376)
        at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1166)
        at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:410)
        at org.apache.cxf.jaxrs.client.AbstractClient.readBody(AbstractClient.java:447)

Any help is greatly appreciated.

like image 540
Chris Avatar asked Aug 30 '12 17:08

Chris


2 Answers

This sounds like a classpath issue: that is, Jackson code can not find named class with its classloader. If possible, maybe you could see where jars for Jackson and value class come from, and see if that would explain it. Classpath issues are quite notorious on servlet container deployments unfortunately.

like image 134
StaxMan Avatar answered Sep 23 '22 04:09

StaxMan


How I solved this problem in my case (which may be completely unrelated to your case):

    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader( classLoader );
    try
    {
        invokeCodeThatPerformsSerializationDeserialization();
    }
    finally
    {
        Thread.currentThread().setContextClassLoader( oldClassLoader );
    }
like image 26
Mike Nakis Avatar answered Sep 24 '22 04:09

Mike Nakis