{"record":{"id":"18125e6187ba49fa","repo":"quarkusio/quarkus","slug":"cannot-create-jsonobject","errorCode":null,"errorMessage":"Cannot create JsonObject","messagePattern":"Cannot create JsonObject","errorType":"http","errorClass":"NoContentException","httpStatus":204,"severity":"error","filePath":"extensions/resteasy-classic/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/vertx/JsonObjectReader.java","lineNumber":37,"sourceCode":"\n/**\n * A body reader that allows to get a Vert.x {@link JsonObject} as JAX-RS request content.\n */\n@Provider\n@Produces(MediaType.APPLICATION_JSON)\npublic class JsonObjectReader implements MessageBodyReader<JsonObject> {\n\n    @Override\n    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {\n        return type == JsonObject.class;\n    }\n\n    @Override\n    public JsonObject readFrom(Class<JsonObject> type, Type genericType, Annotation[] annotations, MediaType mediaType,\n            MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {\n        byte[] bytes = getBytes(entityStream);\n        if (bytes.length == 0) {\n            throw new NoContentException(\"Cannot create JsonObject\");\n        }\n        return new JsonObject(Buffer.buffer(bytes));\n    }\n\n    private static byte[] getBytes(InputStream entityStream) throws IOException {\n        ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        byte[] buffer = new byte[4096];\n        int len;\n        while ((len = entityStream.read(buffer)) != -1) {\n            baos.write(buffer, 0, len);\n        }\n        return baos.toByteArray();\n    }\n}\n","sourceCodeStart":19,"sourceCodeEnd":52,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/resteasy-classic/resteasy/runtime/src/main/java/io/quarkus/resteasy/runtime/vertx/JsonObjectReader.java#L19-L52","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fix the client to send a valid JSON object (e.g. {} at minimum) with Content-Type: application/json.","If an empty body is legitimate, accept String/InputStream and map empty input to a default/empty JsonObject yourself.","Add a NoContentException ExceptionMapper returning 400 with a clear message for better API ergonomics."],"exampleFix":"// before\n@PUT\npublic void update(JsonObject config) { ... } // NoContentException when body empty\n\n// after\n@PUT\npublic void update(String body) {\n    JsonObject config = (body == null || body.isBlank())\n            ? new JsonObject()\n            : new JsonObject(body);\n    ...\n}","handlingStrategy":"validation","validationCode":"byte[] bytes = body == null ? new byte[0] : body.getBytes(StandardCharsets.UTF_8);\nif (bytes.length == 0) {\n    throw new WebApplicationException(\"JsonObject body required\", 400);\n}","typeGuard":"boolean hasJsonObjectBody(jakarta.ws.rs.core.HttpHeaders headers, InputStream in) throws IOException {\n    return headers.getMediaType() != null\n        && headers.getMediaType().isCompatible(jakarta.ws.rs.core.MediaType.APPLICATION_JSON_TYPE)\n        && in.available() > 0;\n}","tryCatchPattern":"try {\n    config = new JsonObject(entityStream.readAllBytes());\n} catch (jakarta.ws.rs.NoContentException e) {\n    config = new JsonObject(); // or abort with 400\n}","preventionTips":["Send a JSON object body (at least {}) with Content-Type: application/json.","Check curl/test client invocations include -d/--data for POST/PUT.","Add empty-body tests for endpoints consuming JsonObject.","Map NoContentException to a 400 with a descriptive message."],"tags":["json","jaxrs","empty-body","vertx"],"backgroundTag":"empty-request-body","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}