quarkusio/quarkus · error · EncodeException

Failed to encode as JSON:

Error message

Failed to encode as JSON: 

What it means

QuarkusJacksonJsonCodec.toBuffer serializes an object to JSON using the Quarkus-configured Jackson ObjectMapper and wraps any Jackson failure in a Jackson's EncodeException with the message 'Failed to encode as JSON: ' plus the underlying cause message. It exists so event bus / JSON codecs surface serialization errors consistently.

Source

Thrown at extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/jackson/QuarkusJacksonJsonCodec.java:170

    }

    @Override
    public String toString(Object object, boolean pretty) throws EncodeException {
        try {
            ObjectMapper mapper = pretty ? prettyMapper() : QuarkusJacksonJsonCodec.mapper();
            return mapper.writeValueAsString(object);
        } catch (Exception e) {
            throw new EncodeException("Failed to encode as JSON: " + e.getMessage(), e);
        }
    }

    @Override
    public Buffer toBuffer(Object object, boolean pretty) throws EncodeException {
        try {
            ObjectMapper mapper = pretty ? prettyMapper() : QuarkusJacksonJsonCodec.mapper();
            return Buffer.buffer(mapper.writeValueAsBytes(object));
        } catch (Exception e) {
            throw new EncodeException("Failed to encode as JSON: " + e.getMessage(), e);
        }
    }

    private static void close(Closeable parser) {
        try {
            parser.close();
        } catch (IOException ignore) {
        }
    }

    @SuppressWarnings("rawtypes")
    private static Object adapt(Object o) {
        try {
            if (o instanceof List) {
                List list = (List) o;
                return new JsonArray(list);
            } else if (o instanceof Map) {
                @SuppressWarnings("unchecked")

View on GitHub (pinned to e1c734241f)

Solutions

  1. Read the cause message appended to 'Failed to encode as JSON:' to identify the offending property
  2. Register missing Jackson modules or annotate types with @JsonSerialize/@JsonIgnore where appropriate
  3. Break cyclic references with @JsonIgnore on the back-reference or @JsonManagedReference/@JsonBackReference
  4. Ensure lazy JPA associations are initialized or DTO-mapped before encoding

Example fix

// before
class Order { User user; }
class User { @JsonIgnore omitted; Order order; } // cycle -> EncodeException

// after
class Order { User user; }
class User { @JsonIgnore Order order; }
Defensive patterns

Strategy: try-catch

Try / catch

try {
    Buffer buf = codec.toBuffer(obj);
} catch (EncodeException e) {
    log.errorf(e, "Cannot serialize %s: %s", obj.getClass(), e.getMessage());
    throw new BadRequestException("Unserializable payload");
}

Prevention

When it happens

Trigger: Calling toBuffer (directly or via event bus publishing/routing with JSON codec) with an object Jackson cannot serialize: no serializer, self-referencing structure, invalid Java types, or a custom getter that throws.

Common situations: Publishing a POJO over the Vert.x event bus in clustered mode or with JSON encoding; missing @JsonProperty or Jackson modules (java.time, jdk8); cyclic object graphs; lazy Hibernate entities serialized outside a session.

Related errors


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