quarkusio/quarkus · error · NoContentException

Cannot create JsonArray

Error message

Cannot create JsonArray

What it means

JsonArrayReader is a JAX-RS MessageBodyReader for io.vertx.core.json.JsonArray. It reads the entity into bytes and throws NoContentException("Cannot create JsonArray") when the request body is empty (0 bytes), since an empty payload cannot be parsed into a JsonArray.

Source

Thrown at extensions/resteasy-classic/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/vertx/JsonArrayReader.java:37

/**
 * A body reader that allows to get a Vert.x {@link JsonArray} as JAX-RS request content.
 */
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JsonArrayReader implements MessageBodyReader<JsonArray> {

    @Override
    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return type == JsonArray.class;
    }

    @Override
    public JsonArray readFrom(Class<JsonArray> type, Type genericType, Annotation[] annotations, MediaType mediaType,
            MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
        byte[] bytes = getBytes(entityStream);
        if (bytes.length == 0) {
            throw new NoContentException("Cannot create JsonArray");
        }
        return new JsonArray(Buffer.buffer(bytes));
    }

    private static byte[] getBytes(InputStream entityStream) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int len;
        while ((len = entityStream.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        return baos.toByteArray();
    }
}

View on GitHub (pinned to e1c734241f)

Solutions

  1. Make the resource method accept no-body callers explicitly (read String/InputStream yourself and treat empty as an empty array or 400).
  2. Fix the client to always send a valid JSON body, e.g. [] for an empty array, and set Content-Type: application/json.
  3. Return a clearer client error: add an exception mapper for NoContentException returning 400 with a helpful message.

Example fix

// before
@POST
public Response add(JsonArray items) { ... } // NoContentException on empty body

// after
@POST
public Response add(String body) {
    if (body == null || body.isBlank()) {
        return Response.status(400).entity("body required").build();
    }
    JsonArray items = new JsonArray(body);
    ...
}
Defensive patterns

Strategy: validation

Validate before calling

byte[] bytes = body == null ? new byte[0] : body.getBytes(StandardCharsets.UTF_8);
if (bytes.length == 0) {
    throw new WebApplicationException("JsonArray body required", 400);
}

Type guard

boolean hasJsonArrayBody(jakarta.ws.rs.core.HttpHeaders headers, InputStream in) throws IOException {
    return headers.getMediaType() != null
        && headers.getMediaType().isCompatible(jakarta.ws.rs.core.MediaType.APPLICATION_JSON_TYPE)
        && in.available() > 0;
}

Try / catch

try {
    items = new JsonArray(entityStream.readAllBytes());
} catch (jakarta.ws.rs.NoContentException e) {
    items = new JsonArray(); // or abort with 400
}

Prevention

When it happens

Trigger: A JAX-RS resource method consumes a JsonArray body (@Consumes(APPLICATION_JSON)) but the client sends no body (Content-Length: 0 or empty chunked body), triggering readFrom with an empty entity stream.

Common situations: HTTP clients sending POST/PUT without a body, wrong Content-Type causing the body to be dropped, gateways/tests stripping payloads, clients intending to send [] but sending nothing due to a serialization bug.

Related errors


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