0

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 Stringrequest 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);
    }
}

Architecture

the_tech_maddy
  • 575
  • 2
  • 6
  • 21
  • 1
    Have you checked that the proxy is forwarding the body? From your error, it seems you try to parse an empty String, instead of a JSON – Slimu Mar 19 '20 at 10:51
  • Did that. Again the same – the_tech_maddy Mar 19 '20 at 10:53
  • You should read the InputStream into a String and print that result, then post it here. See this https://stackoverflow.com/questions/309424/how-do-i-read-convert-an-inputstream-into-a-string-in-java – Slimu Mar 19 '20 at 10:57
  • That's very huge JSON. If I post here, it would be ugly. The problem is direct parsing not working. – the_tech_maddy Mar 19 '20 at 11:02
  • So you are saying that you are receiving in the Micro Service the JSON sent by the Spring Boot Application. In the String value printed above, is the whole value printed on a single line? Maybe you have a newline char on the first line, or inside the JSON String – Slimu Mar 19 '20 at 11:15
  • I have checked that also now. I'm curious to know that only after parsing to `String`, I could able to parse to `MyPojo` – the_tech_maddy Mar 19 '20 at 11:37

0 Answers0