java data_error ai_generated true

com.fasterxml.jackson.core.JsonParseException: 意外字符 ('<' (代码 60)): 期望一个有效值(JSON 字符串、数字、数组、对象或标记 'null'、'true' 或 'false')

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

ID: java/jackson-jsonparseexception-unexpected-char

其他格式: JSON · Markdown 中文 · English
85%修复率
88%置信度
1证据数
2024-04-12首次发现

版本兼容性

版本状态引入弃用备注
Jackson 2.13 active
Jackson 2.14 active
Jackson 2.15 active
Jackson 2.16 active

根因分析

Jackson 尝试解析包含非 JSON 内容的响应或输入,通常是一个 HTML 页面(以 '<' 开头)而不是有效的 JSON,通常是由于错误的 URL、服务器错误响应或缺少 API 端点。

English

Jackson is trying to parse a response or input that contains non-JSON content, typically an HTML page (starting with '<') instead of valid JSON, often due to an incorrect URL, server error response, or missing API endpoint.

generic

官方文档

https://fasterxml.github.io/jackson-core/javadoc/2.14/com/fasterxml/jackson/core/JsonParseException.html

解决方案

  1. Check the actual response from the server by logging the raw response string before parsing. For example: System.out.println("Response: " + responseBody); and verify the URL or API endpoint.
  2. Use a debug proxy like Fiddler or Charles Proxy to capture the HTTP response and confirm it's HTML instead of JSON. Then fix the request URL or handle HTTP error codes (e.g., 404, 500) gracefully.
  3. Add a response content-type check before parsing: if (response.getHeader("Content-Type").contains("application/json")) { parse; } else { handle error; }

无效尝试

常见但无效的做法:

  1. Add @JsonIgnoreProperties(ignoreUnknown = true) to the POJO class 95% 失败

    This annotation ignores unknown JSON properties but does not help when the input is not JSON at all (e.g., HTML).

  2. Increase the Jackson parsing leniency with ObjectMapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES) 90% 失败

    These features relax JSON syntax rules but still require valid JSON structure; they cannot parse HTML.

  3. Wrap the parsing in a try-catch and return an empty object 80% 失败

    This hides the error but does not address the root cause (the server returning HTML). The application may behave incorrectly.