Spring rejects a JSON byte[] field because the string is not Base64 Jackson accepts
Your error
JSON parse error: Cannot deserialize value of type `byte[]` from String "data:image/png;base64,iVBORw0KGgo...": Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character ':' (code 0x3a) in base64 content2 more lines it can showFix it fast
Most common cause, 1 of 3The client sends a data: URL instead of bare Base64
Cut everything up to and including the first comma before sending, so the field carries only the encoded bytes.
const reader = new FileReader();
reader.addEventListener('load', () => {
const base64 = String(reader.result).split(',')[1];
fetch('/api/files', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: file.name, data: base64 }),
});
});
reader.readAsDataURL(file);Short answer
Jackson turns a JSON string into byte[] only by decoding it as standard, padded Base64 (the MIME-NO-LINEFEEDS variant). It fails when the value starts with a data: URL prefix, uses the URL-safe - and _ characters, has its padding stripped, or is not Base64 at all. Send bare padded Base64 or set a matching Base64Variant. Jackson 2.23 and 3.3.
Which cause do you have?
Pull the part of the message that names the problem. A ':' points to cause 1, a '-' or '_' to cause 2, and the padding text to cause 3. Any other character means the client is not sending Base64 at all.
grep -oE "Illegal character '.' \(code 0x[0-9a-f]+\)|expects padding" app.log | sort | uniq -cThen find the line you see in this list and open its fix:
- 1The client sends a data: URL instead of bare Base64
Illegal character ':' (code 0x3a), and the quoted string in the message begins with data: - 2The value uses the URL-safe alphabet
Illegal character '-' (code 0x2d) or Illegal character '_' (code 0x5f) - 3The trailing = padding was stripped
Unexpected end of base64-encoded String ... expects padding (one or more '=' characters) at the end
What does the error look like?
JSON parse error: Cannot deserialize value of type `byte[]` from String "data:image/png;base64,iVBORw0KGgo...": Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character ':' (code 0x3a) in base64 contentFailed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '-' (code 0x2d) in base64 contentUnexpected end of base64-encoded String: base64 variant 'MIME-NO-LINEFEEDS' expects padding (one or more '=' characters) at the end. This Base64Variant might have been incorrectly configured
Cause 1 of 3
The client sends a data: URL instead of bare Base64
You seeIllegal character ':' (code 0x3a), and the quoted string in the message begins with data:
Check
Cannot deserialize value of type byte[] with Illegal character ':' means the JSON value holds a whole data: URL, such as data:image/png;base64, followed by the payload. FileReader.readAsDataURL in the browser returns that form, and MDN says the declaration has to be removed before the rest decodes as Base64.
grep -o 'from String "data:[^,]*,' app.log | head -n 3Fix
Cut everything up to and including the first comma before sending, so the field carries only the encoded bytes.
const reader = new FileReader();
reader.addEventListener('load', () => {
const base64 = String(reader.result).split(',')[1];
fetch('/api/files', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: file.name, data: base64 }),
});
});
reader.readAsDataURL(file);Verify
The upload returns a 2xx status and the stored byte[] has the same length as the original file.
curl -s -o /dev/null -w '%{http_code}\n' -H 'Content-Type: application/json' -d '{"name":"a.txt","data":"aGVsbG8="}' http://localhost:8080/api/filesConfirmed by developer.mozilla.org
Cause 2 of 3
The value uses the URL-safe alphabet
You seeIllegal character '-' (code 0x2d) or Illegal character '_' (code 0x5f)
Check
Jackson decodes byte[] fields with its default variant, MIME-NO-LINEFEEDS, which keeps the standard + and / characters. Text from a URL-safe encoder replaces those two with - and _, so decoding stops at the first one. Jackson's own URL variant, MODIFIED_FOR_URL, uses - and _ and no padding.
grep -o 'Illegal character .\(-\|_\)' app.log | head -n 3Fix
Encode with the standard alphabet on the sender, or switch the mapper's default variant. On Spring Boot 4 (Jackson 3), a JsonMapperBuilderCustomizer bean sets it; with Jackson 2, call setBase64Variant on the ObjectMapper. The variant then applies to every byte[] field the mapper reads and writes, and it rejects = padding.
import org.springframework.boot.jackson.autoconfigure.JsonMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tools.jackson.core.Base64Variants;
@Configuration
class JacksonBase64Config {
@Bean
JsonMapperBuilderCustomizer urlSafeBase64() {
return builder -> builder.defaultBase64Variant(Base64Variants.MODIFIED_FOR_URL);
}
}Verify
The same request with - and _ in the value is accepted, and the decoded bytes match what the sender encoded.
curl -s -w '\n%{http_code}\n' -H 'Content-Type: application/json' -d '{"name":"a.bin","data":"-_-_"}' http://localhost:8080/api/filesConfirmed by raw.githubusercontent.com
Cause 3 of 3
The trailing = padding was stripped
You seeUnexpected end of base64-encoded String ... expects padding (one or more '=' characters) at the end
Check
Jackson's built-in variants that write padding also require it when reading. Base64 whose length is not a multiple of four, with the closing = characters dropped by the sender, ends mid-unit and fails with the padding message.
grep -c "expects padding" app.logFix
Have the sender keep padding (in Java, avoid Base64.getEncoder().withoutPadding()), or relax the read rule with withPaddingAllowed() on the default variant. Another route is to map the field as String and decode it yourself: java.util.Base64 decoders accept input without padding.
public record FileUpload(String name, String data) {
public byte[] bytes() {
return java.util.Base64.getDecoder().decode(data);
}
}Verify
A value such as aGVsbG8 (hello without its =) now decodes to 5 bytes.
byte[] b = java.util.Base64.getDecoder().decode("aGVsbG8");
System.out.println(b.length); // 5Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Write down in the API contract that byte[] fields carry standard Base64 with padding and no data: prefix, and reject other input in a client-side check.
- Add one controller test that posts a known value such as aGVsbG8= and asserts the decoded bytes, so a mapper change shows up in CI.
- If one endpoint needs URL-safe input, use a String field and java.util.Base64.getUrlDecoder() there instead of changing the default variant for the whole app.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Dispatch Pro.