com.fasterxml.jackson.databind.exc.MismatchedInputException:

I'm posting json string in the request body with escape characters and mapping it to a model in the controller but it is throwing below exception

json string : "{\"id\":\"1\",\"desc\":\"test\"}" exception can not construct instance of Entity (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"id":"1","desc":"test"}') at [Source: (String)" "{\"id\":\"1\",\"desc\":\"test\"}""; line: 2, column: 2]"

but when i use a json string with out escape characters it is working fine json string

{"id":"1","desc":"test"}

Model

class Entity
{
@Id 
String id ;
Map<String, Object> dynamicFields = new LinkedHashMap<>();

@JsonAnySetter
void setDetail(String key, Object value) {
    dynamicFields.put(key, value);
}


public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public Map<String, Object> getDynamicFields() {
    return dynamicFields;
}

public void setDynamicFields(Map<String, Object> dynamicFields) {
    this.dynamicFields = dynamicFields;
}

and below is the controller post method

@PostMapping("")
ResponseEntity<Entity> create(@RequestBody String jsonString) throws JsonParseException, 
JsonMappingException, IOException {
    objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
     Entity mongoStoredEnity = objectMapper.readValue(jsonString,  Entity.class);
//rest of the code ....
}

any clue on this why it is accepting json string with out escape characters only?

TLDR: Don't send the leading quotation marks, else the value is interpreted as a string.

You input the wrong value, i.e. your input is actually "\"{\"id\":\"1\",\"desc\":\"test\"}\"" instead of the correct value "{\"id\":\"1\",\"desc\":\"test\"}". I.e. you need to remove the leading and trailing ".

EDIT: If you want to test it:

public static void main(final String[] args) throws JsonParseException, JsonMappingException, IOException {
    final ObjectMapper om = new ObjectMapper();
    om.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
    final String jsonString = "\"{\"id\":\"1\",\"desc\":\"test\"}\""; // this throws your MismatchedInputException
    final String jsonStringCorrect = "{\"id\":\"1\",\"desc\":\"test\"}"; // this works as expected
    final Entity mongoStoredEnity = om.readValue(jsonStringCorrect, Entity.class);
    System.out.println(mongoStoredEnity);
}

MismatchedInputException (jackson-databind 2.9.0 API), [Solved] com.fasterxml.jackson.databind.exc. MismatchedInputException: Cannot deserialize instance of `XYZ` out of START_ARRAY token. com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token Ask Question Asked 3 years, 1 month ago

Try to replace backslash with following code.

String jsonFormattedString = jsonString.replaceAll("\\\\", "");

Then pass it to Object Mapper

[Solved] com.fasterxml.jackson.databind.exc , com.fasterxml.jackson.databind.exc.MismatchedInputException. Using jackson- databind to deserialize JSON into List but hits the below´┐Ż com.fasterxml.jackson.databind.exc.MismatchedInputException: Can not deserialize instance of object out of START_ARRAY token 0 Unexpected token: ) in findAll and findByKeyIn with Spring and Eclipselink JPA

You can use this:

myString.replace("\"{", "{").replace("}\"", "}")

With that, you replace the first and the end quote.

MismatchedInputException (The Adobe AEM Quickstart and Web , fasterxml.jackson.databind.exc.MismatchedInputException: no String-argument constructor/factory method to deserialize from String value #278. com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.order.soap.Something (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('3050') which is really weird. Coz I have other elements with just attribute value and all of them work just fine.

com.fasterxml.jackson.databind.exc.MismatchedInputException java , The most common Jackson exceptions - the cause of the problem and the solution for each com.fasterxml.jackson.databind. at c.f.j.d.exc. Specialized sub-class of MismatchedInputException that is used when the underlying problem appears to be that of bad formatting of a value to deserialize.

com.fasterxml.jackson.databind.exc.MismatchedInputException: no , Specialized sub-class of MismatchedInputException that is used when the underlying problem appears to be that of bad formatting of a value to deserialize. yes it requires however the exception is thrown by com.fasterxml.jackson.databind.exc.MismatchedInputException Also this issue happens on Wrapper objects that are sent to the controllers (that has no relation to Hibernate).

Jackson Exceptions, com.fasterxml.jackson.databind.exc MismatchedInputException Javadoc General exception type used as the base class for all JsonMappingExceptions that are due to input not mapping to target definition; these are typically considered "client errors" since target type definition itself is not the root cause but mismatching input.

Comments
  • The code works for me if I hardcode objectMapper.readValue("{\"id\":\"1\",\"desc\":\"test\"}", Entity.class);. My guess is that the passed in jsonString contains something different, maybe escape characters. Can you debug the actual value of jsonString?
  • i'm sending it from google arc client and actual value is coming correct only you can observe it in the exception message it self
  • Possible duplicate of How to UNESCAPE String while converting java object to JSON string using Jackson
  • no chance read the question carefully.I'm not able to map to a pojo when i send a valid json string with escape characters from a rest client
  • no i'm sending json like this from google arc client "{\"id\":\"1\",\"desc\":\"test\"}\"
  • sure, please remove the leading and trailing quotation marks to make it a json object, i.e. just input {\"id\":\"1\",\"desc\":\"test\"}
  • if i pass like that rest client will complain that it is not a valid json.You can check online whether it is a valid json or not
  • Ah, well, yes, but interestingly it's ok for jackson. However, "invalid{}//json[[[" or anything else that is a valid string is valid json, but you require the object.
  • Also: Why do you have those escape chars? And why don't you just have @RequestBody Entity mongoStoredEnity ?
  • i'm passing a valid json it should work with escape characters also .replacing backslash is an extra operation and will cause performance issues and i'm sending a long json string in my actual context