quarkusio/quarkus · error · NoContentException

Cannot create JsonObject

Error message

Cannot create JsonObject

What it means

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

Source

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

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

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

    @Override
    public JsonObject readFrom(Class<JsonObject> 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 JsonObject");
        }
        return new JsonObject(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. Fix the client to send a valid JSON object (e.g. {} at minimum) with Content-Type: application/json.
  2. If an empty body is legitimate, accept String/InputStream and map empty input to a default/empty JsonObject yourself.
  3. Add a NoContentException ExceptionMapper returning 400 with a clear message for better API ergonomics.

Example fix

// before
@PUT
public void update(JsonObject config) { ... } // NoContentException when body empty

// after
@PUT
public void update(String body) {
    JsonObject config = (body == null || body.isBlank())
            ? new JsonObject()
            : new JsonObject(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("JsonObject body required", 400);
}

Type guard

boolean hasJsonObjectBody(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 {
    config = new JsonObject(entityStream.readAllBytes());
} catch (jakarta.ws.rs.NoContentException e) {
    config = new JsonObject(); // or abort with 400
}

Prevention

When it happens

Trigger: A resource method consuming a JsonObject body (@Consumes(APPLICATION_JSON)) receives a request with no body — Content-Length: 0, empty streamed body, or a client that failed to serialize the entity.

Common situations: POST/PUT calls with omitted body from scripts/tests, curl commands missing -d, HTTP clients silently skipping empty objects, proxies or Content-Type mismatches causing the body to be dropped.

Related errors


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