-1

I have my own object when there is no value it is coming as "NULL" in a string from downstream and it is failing while converting to java object in ObjectMapper

class Test 
private String value1; 
private String value2; 
.... getters and setters.

Json Response is

{"Test": "NULL"}

How I can handle this in java Error message is :

** error : com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.test.Test (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('NULL') **

This is how I am converting json to java

ResponseEntity<String> responseEntity = getDownstreamResponse(id);
ObjectMapper mapper = new ObjectMapper();
Test test =  mapper.readValue(responseEntity.getBody(), Test.class);
7
  • ObjectMapper are related to your get/set and constructor methods since thats how they scan the fields and possibilities. In this case the defined json-string (response) does not match the object at the top so it can not be mapped correct. You need to make sure that it can be built with all information being present and also for ignoring null values, etc. Commented Feb 14, 2024 at 7:32
  • Similar: stackoverflow.com/questions/36227224/… Commented Feb 14, 2024 at 7:33
  • Can you show the code that you use to parse the response (and not only the property Test)? Commented Feb 14, 2024 at 9:14
  • @MatteoNNZ I have updated the post could you please check that. Commented Feb 14, 2024 at 10:04
  • Where is that JSON coming from? It's clearly a string field that has the value "NULL", not a _null_/empty value. Thus, trying to parse it as a class with multiple fields cannot work. That is, there's a mismatch between the JSON and what you're expecting. Commented Feb 14, 2024 at 13:49

1 Answer 1

1

Inside the body of:

ResponseEntity<String> responseEntity = getDownstreamResponse(id);

... you will have:

{"Test": "NULL"}

... but that's not a Test.class. The Test.class, eventually, it's only the value of the key "Test", so the following line of code would fail anyway even if the response was correct:

Test test =  mapper.readValue(responseEntity.getBody(), Test.class);

To make it simpler, I'd do this:

ObjectMapper mapper = new ObjectMapper();
JsonNode fullResponse = mapper.readTree(responseEntity.getBody());
JsonNode testNode = fullResponse.get("Test");
if ("NULL".equals(testNode.asText())) {
    //you won't be able to parse the response into class Test.class. 
    //do what you wish to do here (throw an exception, return null etc.)
}
Test test =  mapper.readValue(testNode, Test.class);
//here you should have parsed test, so do what you want with it
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.