quarkusio/quarkus · error · BadRequestException

Invalid character:

Error message

Invalid character: 

What it means

CharacterMessageBodyHandler.doReadFrom throws a jakarta.ws.rs.BadRequestException when the response/entity body is not exactly one character long. A Java char can only hold a single character, so any entity stream that decodes to a string of length != 1 cannot be converted to a Character.

Source

Thrown at independent-projects/resteasy-reactive/common/runtime/src/main/java/org/jboss/resteasy/reactive/common/providers/serialisers/CharacterMessageBodyHandler.java:29

import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyReader;

public class CharacterMessageBodyHandler extends PrimitiveBodyHandler implements MessageBodyReader<Character> {
    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return type == Character.class;
    }

    public Character readFrom(Class<Character> type, Type genericType, Annotation[] annotations, MediaType mediaType,
            MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
            throws IOException, WebApplicationException {
        return doReadFrom(entityStream);
    }

    protected char doReadFrom(InputStream entityStream) throws IOException {
        String string = readFrom(entityStream, false);
        if (string.length() == 1)
            return string.charAt(0);
        throw new BadRequestException("Invalid character: " + string);
    }
}

View on GitHub (pinned to e1c734241f)

Solutions

  1. Change the expected entity type to String and validate/extract the single character yourself
  2. Fix the server endpoint to return exactly one character for Character consumers
  3. Check for empty/oversized bodies before requesting a Character entity

Example fix

// before
char c = client.target(url).request().get(char.class);
// after
String s = client.target(url).request().get(String.class);
if (s == null || s.length() != 1) throw new IllegalStateException("Expected single char, got: " + s);
char c = s.charAt(0);
Defensive patterns

Strategy: try-catch

Validate before calling

String body = response.readEntity(String.class);
if (body == null || body.length() != 1) { throw new IllegalStateException("Expected single-character entity"); }

Type guard

boolean isSingleChar(String s) { return s != null && s.length() == 1; }

Try / catch

try {
    char c = response.readEntity(char.class);
} catch (BadRequestException e) {
    // body not exactly one character; fall back to String
    String s = response.readEntity(String.class);
}

Prevention

When it happens

Trigger: Reading an HTTP entity as Character/char when the server returns an empty body, multi-character text (e.g. "ab", a word), or whitespace-padded content.

Common situations: REST clients declaring Character as the response type against endpoints that return JSON strings or longer text; endpoints that changed their payload shape; empty 200 responses.

Understand the failure class

Related errors


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