OpenFeign/feign · error · IOException
Cannot convert to type
Error message
Cannot convert to type: ${type.getTypeName()} What it means
JacksonJrDecoder.convert (the Encoder-side conversion of an object to the requested Type) throws IOException when the target type is neither a handled ParameterizedType nor a plain Class — mapper.beanFrom can only target concrete classes. It mirrors findTransformer's DecodeException but on the object-to-JSON conversion path.
Solutions
- Pass a concrete bean Class as the target type.
- Use the full feign-jackson encoder/decoder for complex generic types.
- Convert the object to a concrete DTO before encoding.
- Provide a custom Encoder for the unsupported type.
Example fix
// before encoder.encode(obj, MyApiInterface.class, template); // interface type // after encoder.encode(obj, UserDto.class, template); // concrete bean class
Defensive patterns
Strategy: type-guard
Validate before calling
static boolean jacksonJrCanEncode(Type type) {
if (type instanceof ParameterizedType) type = ((ParameterizedType) type).getRawType();
return type instanceof Class;
} Type guard
static boolean isConcreteClass(Type type) {
if (type instanceof ParameterizedType) type = ((ParameterizedType) type).getRawType();
return type instanceof Class && !((Class<?>) type).isInterface() && !type.equals(Object.class);
} Try / catch
try {
encoder.encode(obj, type, template);
} catch (IOException e) {
if (e.getMessage() != null && e.getMessage().startsWith("Cannot convert to type")) {
throw new IllegalArgumentException("Target type must be a concrete bean class for jackson-jr", e);
}
throw e;
} Prevention
- Pass concrete DTO classes to encoders
- Avoid encoding objects typed as interfaces
- Switch to the full Jackson module for generic-rich types
When it happens
Trigger: Calling JacksonJrEncoder/convert with a target Type that resolves to something other than a Class (interface, generic type variable, array of generics in unsupported shapes).
Common situations: Serializing objects typed to interfaces without concrete classes; generic method signatures where Type resolution yields TypeVariable; using jackson-jr encoder with types the full Jackson module would support.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Cannot decode type
- ${e.getMessage()}
- ${e.getMessage()}
- is not a type supported by this encoder.
- is not a type supported by this decoder.
AI-assisted analysis of OpenFeign/feign@e2a1e27560 (2026-09-10).
Data as JSON: /api/errors/a3cea708b092b625.
Report an issue: GitHub.
Appendix: source
Thrown at jackson-jr/src/main/java/feign/jackson/jr/JacksonJrDecoder.java:137
@Override
public Object convert(Object object, Type type) throws IOException {
String json = mapper.asString(object);
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
Type rawType = pt.getRawType();
Type[] args = pt.getActualTypeArguments();
if (rawType.equals(List.class)) {
return mapper.listOfFrom((Class<?>) args[0], json);
}
if (rawType.equals(Map.class)) {
return mapper.mapOfFrom((Class<?>) args[1], json);
}
type = rawType;
}
if (type instanceof Class) {
return mapper.beanFrom((Class<?>) type, json);
}
throw new IOException("Cannot convert to type: " + type.getTypeName());
}
@Override
public boolean canDecode(Response response, Type type) {
return Util.isJsonContentType(response);
}
}
View on GitHub (pinned to e2a1e27560)