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.
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
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.
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.
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:
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.
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:
else
to set the default only when necessaryIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With