My Spring boot web application internally sends API request to another Spring boot Micro service via a proxy kind of application(also used by other applications in our project) as given in the below image.
The problem is in Jackson Deserialization. In Webapp, the POJO is converted to JSON string using ObjectMapper
and the request is sent as String
in request body. The proxy first receives the request and this String
request body is not touched. But it does other works with the request. The problem occurs when the request is again bypassed to my micro service and when I deserialize this String
request to my required POJO type. This is given below.
// Inside OncePerRequestFilter#doFilterInternal
safeParseJSON(objectMapper, request.getInputStream(), MyPojo.class); // Failed to Parse.
safeParseJSON(objectMapper, safeParseJSON(objectMapper, httpServletRequest.getInputStream(), String.class), MyPojo.class) // Successfully parsed
Getting exception like jackson.databind.JsonMappingException: Can not construct instance of...no String-argument constructor/factory method to deserialize from String value ('<MY HUGE JSON>')
. Please help me on this
public static <T> T safeParseJSON(ObjectMapper objectMapper, InputStream inputStream, Class<T> targetType) {
try {
return objectMapper.readValue(inputStream, targetType);
} catch (IOException ex) {
throw new RuntimeException(String.format("Unable to parse JSON payload - %s", ex.getMessage()), ex);
}
}