Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input

Tags:

java

json

jackson

I need to map a JSON array object with java POJO class. I wrote the code like this:

// execute the client with get method 
InputStream inputStream = getMethod.getResponseBodyAsStream();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

ObjectMapper objectMapper = new ObjectMapper();

JsonFactory jsonFactory = new JsonFactory();
List<OwnerDetail> owners = new ArrayList<>();
JsonParser jsonParser = jsonFactory.createJsonParser(inputStream);

if (jsonParser.nextToken() != null && jsonParser.)
{ // end-of-input
  owners = objectMapper.readValue(bufferedReader, TypeFactory.defaultInstance().constructCollectionType(List.class, OwnerDetail.class));
}

The above block is giving me following error:

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
at [Source: java.io.BufferedReader@5e66c5fc; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3029)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2971)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2128)

Any help would be appreciated. Thanks.

like image 256
Ram Dutt Shukla Avatar asked Nov 24 '14 11:11

Ram Dutt Shukla


People also ask

What is the jsonmappingexception thrown by Jackson?

This exception is thrown if the JSON doesn't match exactly what Jackson is looking for. For example, the main JSON could be wrapped: 4.2. The Solution We can solve this problem using the annotation @JsonRootName: When we try to deserialize the wrapped JSON, it works correctly: 5. JsonMappingException: No Serializer Found for Class 5.1. The Problem

Is there A Serializer for jsonmappingexception?

JsonMappingException: No Serializer Found for Class 5.1. The Problem Now let's take a look at JsonMappingException: No Serializer Found for Class. This exception is thrown if we try to serialize an instance while its properties and their getters are private.

Why am I getting a JSON error in Getmapping?

Mostly you are not setting Content-Type: application/json when you the request. Content-Type: application/x-www-form-urlencoded will be chosen which might be causing this exception. I know this is weird but when I changed GetMapping to PostMapping for both client and server side the error disappeared.

What is jsonmappingexception no suitable constructor?

3. JsonMappingException: No Suitable Constructor 3.1. The Problem Now let's look at the common JsonMappingException: No Suitable Constructor found for type. This exception is thrown if Jackson can't access the constructor. In the following example, class User doesn't have a default constructor:


2 Answers

After reading response the data from the response is consumed. If your code is in interceptor you could try creating response again and return as below:

    Request request = chain.request();
    Response originalResponse = chain.proceed(request);
    final ResponseBody original = originalResponse.body();
    // if(request.url().toString().equalsIgnoreCase(string)){
    if (originalResponse.code() == HttpURLConnection.HTTP_OK) {
        try {
            String response = originalResponse.body().string();
            JSONObject mainObject = new JSONObject(response);

            // your mapping - manipulation code here.
            originalResponse = originalResponse.newBuilder()
                            .header("Cache-Control", "max-age=60")
                            .body(ResponseBody.create(original.contentType(),
                                    mainObject.toString().getBytes()))
                            .build();

        } catch (JSONException | IOException e) {
            e.printStackTrace();
        }
    }
    return originalResponse;

Here response is created again and returned.

Do let me know any update.

like image 103
DearDhruv Avatar answered Oct 26 '22 05:10

DearDhruv


The use of both inputStream and bufferedReader seems wrong. If you've made a JsonParser for the input, it is probably best to continue using it for the object mapping. There might be a problem with the buffered reader eagerly reading all the content from the stream, leaving nothing for the JSON parser to read.

There also looks to be a section missing from your code, in the if condition.

Something like this:

    if (parser.isExpectedStartArrayToken()) {
        owners = mapper.readValue(parser, mapper.getTypeFactory().constructCollectionType(List.class, OwnerDetail.class));
    }

Extra notes:

  • It's recommended not to wrap streams in buffering/decoding (InputStreamReader) before passing to Jackson as Jackson's parsers have heavily optimised this process already
    • Actually, wrapping InputStreamReader in BufferedReader is practically always redundant as InputStreamReader does buffer content
  • TypeFactory and JsonFactory can both be obtained from an ObjectMapper instance
  • No need to initialise owners to an empty list that will get overridden anyway-- declare it without initialisation and use an else to set the default only when necessary
like image 20
araqnid Avatar answered Oct 26 '22 06:10

araqnid