quarkusio/quarkus · error · IllegalArgumentException

Failed to serialize content of type

Error message

Failed to serialize content of type 

What it means

During EntityPart.build(), the content is serialized with the matching MessageBodyWriter. If the writer throws an IOException while writing to the internal buffer, the builder wraps it in an IllegalArgumentException reading 'Failed to serialize content of type <rawType.getName()>'. This indicates the registered writer for the part's media type failed to produce bytes for the given object.

Source

Thrown at independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/jaxrs/EntityPartBuilderImpl.java:162

        } else if (content instanceof byte[]) {
            return new ByteArrayInputStream((byte[]) content);
        } else if (serialisers != null) {
            MessageBodyWriter<T> writer = null;
            List<MessageBodyWriter<?>> writers = serialisers.findWriters(null, rawType, mediaType);
            for (MessageBodyWriter<?> w : writers) {
                if (w.isWriteable(rawType, genericType, EMPTY_ANNOTATIONS, mediaType)) {
                    writer = (MessageBodyWriter<T>) w;
                    break;
                }
            }
            if (writer != null) {
                try {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    writer.writeTo(content, rawType, genericType, EMPTY_ANNOTATIONS, mediaType,
                            new QuarkusMultivaluedHashMap<>(), baos);
                    return new ByteArrayInputStream(baos.toByteArray());
                } catch (IOException e) {
                    throw new IllegalArgumentException("Failed to serialize content of type " + rawType.getName(), e);
                }
            } else {
                return new ByteArrayInputStream(content.toString().getBytes(StandardCharsets.UTF_8));
            }
        } else {
            return new ByteArrayInputStream(content.toString().getBytes(StandardCharsets.UTF_8));
        }
    }

    @Override
    public EntityPart build() throws IllegalStateException, IOException, WebApplicationException {
        if (content == null) {
            throw new IllegalStateException("No content has been set");
        }

        InputStream resolvedContent;
        if (content instanceof InputStream) {
            resolvedContent = (InputStream) content;

View on GitHub (pinned to e1c734241f)

Solutions

  1. Inspect the wrapped IOException cause (e.getCause()) to find the underlying writer failure and fix the object or writer.
  2. Verify the media type set on the part matches a writer that fully supports the content type (e.g. application/json with JSON-B/Jackson registered).
  3. Simplify the part content: serialize the object to String/byte[] yourself first, then pass that as content.
  4. If a custom MessageBodyWriter is involved, fix its writeTo to not throw raw IOException for logical errors.

Example fix

// before
EntityPart part = EntityPart.withName("payload")
        .content(myUnserializableObject)
        .mediaType("application/json")
        .build(); // throws IllegalArgumentException wrapping IOException
// after
String json = JsonbBuilder.create().toJson(safeDto);
EntityPart part = EntityPart.withName("payload")
        .content(json, new GenericType<String>() {})
        .mediaType("application/json")
        .build();
Defensive patterns

Strategy: try-catch

Validate before calling

// Pre-serialize to verify the object can be converted
byte[] probe = content.toString().getBytes(StandardCharsets.UTF_8); // fallback path sanity

Try / catch

try {
    EntityPart part = builder.build();
} catch (IllegalArgumentException e) {
    if (e.getMessage() != null && e.getMessage().startsWith("Failed to serialize content of type")) {
        LOG.errorf(e.getCause(), "EntityPart serialization failed; cause: %s", e.getCause());
        // re-serialize as String or fix writer
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling build() on an EntityPart whose content object's MessageBodyWriter throws IOException — e.g. a custom writer writing through a stream that fails, or Jackson/JSON-B choking mid-serialization on the part object with the declared media type (application/json etc.).

Common situations: Serializing a POJO whose getter throws or whose nested Jackson serialization fails (unwrapped streams, invalid @JsonValue), custom MessageBodyWriter implementations with I/O bugs, or a media type registered that has no reliable writer for the object.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/b067e0b34590eec6. Report an issue: GitHub.