2
votes

I'm getting a HttpMessageNotReadableException when I try to do a get with spring in java. The Stacktrace printed says:

nested exception is org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Illegal character ((CTRL-CHAR, code 31)): only regular white space (\r, \n, \t) is allowed between tokens

Has it something to do with the way I do the get, or something to do with the format of my response?

The way I'm calling it is:

    HttpEntity<JSONObject> entity = new HttpEntity<JSONObject>();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.set("Accept", "application/x-gzip");
    headers.set("charset", "UTF-8");
    return getConnector().exchange(url, HttpMethod.GET, entity, JSONObject.class);

getConnector() returns a RestConnector

The response headers include: Content-Encoding: gzip Content-Type: application/json

I don't know if it has something to do with the encoding being gzip

EDIT: The problem was that the content-encoding was gzip..finally, I used HttpClient which supports gzip encoding

2

2 Answers

0
votes

This is most probably caused by your response. If it is a regular (static) file or manually constructed, check for illegal white space.

0
votes

In my case, as sprint mvc w/ jackson (jackson-mapper-asl, v-1.9.10) deserialization requires JSON parser. And jackson requires a default constructor for http request message deserialization, if there's no default constructor, jackson will have a problem w/ reflection and throws HttpMessageNotReadableException exception.

This is to say, all the classes/sub-classes which used as Request body, requires a default constructor. This costed me a few moments after I tried adding custom converter and other suggestions I got in stackoverflow in vain.

Or you can add Custom Deserializer or Mixin annotation to avoid adding default constructor hierachically everywhere. as described here: http://blogs.jbisht.com/blogs/2016/09/12/Deserialize-json-with-Java-parameterized-constructor. Check this if you're interested.