FasterXML/jackson-databind · error · IllegalArgumentException

Cannot deserialize Class {} (of type {}) as a Bean

Error message

Cannot deserialize Class {} (of type {}) as a Bean

What it means

Thrown by BeanDeserializerFactory.isPotentialBeanType when Jackson is asked to build a bean deserializer for a type that can never be a bean — anything ClassUtil.canBeABeanType flags (arrays, enums, primitives, interfaces, abstract classes, etc.). These need dedicated deserializers, not bean introspection.

Source

Thrown at src/main/java/tools/jackson/databind/deser/BeanDeserializerFactory.java:1068

    /*
    /**********************************************************
    /* Helper methods for Bean deserializer, other
    /**********************************************************
     */

    /**
     * Helper method used to skip processing for types that we know
     * cannot be (i.e. are never consider to be) beans:
     * things like primitives, Arrays, Enums, and proxy types.
     *<p>
     * Note that usually we shouldn't really be getting these sort of
     * types anyway; but better safe than sorry.
     */
    protected boolean isPotentialBeanType(Class<?> type)
    {
        String typeStr = ClassUtil.canBeABeanType(type);
        if (typeStr != null) {
            throw new IllegalArgumentException("Cannot deserialize Class "+type.getName()+" (of type "+typeStr+") as a Bean");
        }
        if (ClassUtil.isProxyType(type)) {
            throw new IllegalArgumentException("Cannot deserialize Proxy class "+type.getName()+" as a Bean");
        }
        // [databind#3229]: Local/anonymous classes cannot be instantiated but
        //   can still be updated via `readerForUpdating()`. So let them through
        //   here; if actual instantiation is attempted, `ValueInstantiator` will
        //   fail with a clear error.
        return true;
    }

    /**
     * Helper method that will check whether given raw type is marked as always ignorable
     * (for purpose of ignoring properties with type)
     */
    protected boolean isIgnorableType(DeserializationContext ctxt, BeanPropertyDefinition propDef,
            Class<?> type, Map<Class<?>,Boolean> ignoredTypes)
    {

View on GitHub (pinned to a50c7d2a1d)

Solutions

  1. Use the correct target type — arrays via mapper.readValue(json, new TypeReference<int[]>(){}), enums via the enum class, etc.
  2. For interfaces/abstract types, enable polymorphic type info (@JsonTypeInfo) or supply a concrete subtype.
  3. Ensure the TypeReference/JavaType carries the intended concrete class, not a raw or erased type.

Example fix

// before
Object o = mapper.readValue(json, Object.class); // generic erosion -> non-bean path
// after
List<Item> items = mapper.readValue(json, new TypeReference<List<Item>>(){});
Defensive patterns

Strategy: validation

Validate before calling

JavaType t = mapper.constructType(targetTypeReference);
if (t.isArrayType() || t.isEnumType() || t.isPrimitive()
        || t.isInterface() || t.isAbstract()) {
    throw new IllegalArgumentException("Target " + t + " cannot be deserialized as a bean");
}

Prevention

When it happens

Trigger: Asking the mapper to deserialize into an array type (int[]), an enum, a primitive/primitive-wrapper mismatch, or an interface/abstract class without polymorphic type info — usually because the target JavaType resolved to something non-bean.

Common situations: Generic code that passes Class<?> through and lands on a non-bean; type erasure losing the real type; wrong TypeReference; misconfigured @JsonTypeInfo on an interface; passing Object.class or a raw Collection type.

Related errors


AI-assisted analysis of FasterXML/jackson-databind@a50c7d2a1d (2026-08-06). Data as JSON: /api/errors/2570014b787a19ae. Report an issue: GitHub.