sunmingtao / sample-code

3 stars 4 forks source link

JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens #268

Closed sunmingtao closed 3 years ago

sunmingtao commented 3 years ago

Use Spring RestTemplate to parse json from https://projects.fivethirtyeight.com/2018-nba-predictions/data.json

Caused by: com.fasterxml.jackson.core.JsonParseException: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens
 at [Source: (PushbackInputStream); line: 1, column: 2]
    at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1851)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:707)
    at com.fasterxml.jackson.core.base.ParserMinimalBase._throwInvalidSpace(ParserMinimalBase.java:685)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._skipWSOrEnd(UTF8StreamJsonParser.java:3007)
    at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:719)
sunmingtao commented 3 years ago

The solution is

RestTemplate restTemplate = new RestTemplate();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());

The HttpComponentsClientHttpRequestFactory supports gzip encoding https://stackoverflow.com/questions/42621547/jackson-error-illegal-character-only-regular-white-space-allowed-when-parsi/56533517#56533517