Manikiran Bodepudi
Manikiran Bodepudi

Reputation: 69

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?

Upvotes: 1

Views: 7789

Answers (3)

Alexander Ochoa
Alexander Ochoa

Reputation: 79

You can use this:

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

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

Upvotes: 0

sfiss
sfiss

Reputation: 2319

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

Upvotes: 1

Swarit Agarwal
Swarit Agarwal

Reputation: 2648

Try to replace backslash with following code.

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

Then pass it to Object Mapper

Upvotes: 0

Related Questions