2

If I got will get an unsure rows JSON data. How should I set of my class?

This is my current class

public class ChatMessage {

private Map<String, String> message = new HashMap<>();

@JsonAnyGetter
public Map<String, String> any(){
    return this.message;
}

public Map<String, String> getMessage() {
    return this.message;
}

@JsonAnySetter
public void setMessage(String key, String value) {
    message.put(key, value);
}

@Override
public String toString() {
    return "Message [message=" + message + "]";
}
}

this is my json send from js

{"type":"message","user":"james","to":"","message":"Hi every"}

I got error now

org.springframework.messaging.converter.MessageConversionException: Could not 
read JSON: Cannot construct instance of `java.util.LinkedHashMap` (although 
at least one Creator exists): no String-argument constructor/factory method 
to deserialize from String value ('Welcome james join the room')
at [Source: (byte[])" . 
{"type":"message","user":"james","to":"","message":"Welcome james join the 
room"}"; line: 1, column: 52] (through reference chain: 
chat.model.ChatMessage["message"]); nested exception is         
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct         
instance of `java.util.LinkedHashMap` (although at least one Creator exists): 
no String-argument constructor/factory method to deserialize from String 
value ('Welcome james join the room')
at [Source: (byte[])" . 
{"type":"message","user":"james","to":"","message":"Welcome james join the 
room"}"; line: 1, column: 52] (through reference chain: 
chat.model.ChatMessage["message"])

because my json would be like

{"type":"message",
 "user":"james",
 "to":"",
 "message":"Welcome james join the room",
 "xxx":"xxxxxxxxxxxxx"}

or

{"type":"message",
 "user":"james",
 "to":"",
 "message":"Welcome james join the room",
 "yyy":"xxxxxxxxxxxxx"}

how should I set my class? Thx

1
  • how is this working? did you tried to check after converting to object? Commented May 30, 2019 at 6:07

1 Answer 1

2

Rename either the message field in the json or message field in the POJO. As per your POJO structure, jackson is trying to deserialize "message":"Hi every" to the message field in your POJO and is not able to create a Map out of it (Since it's just a String).

Your code will work with following json:

{"type":"message","user":"james","to":"","json-message":"Hi every"}

OR

following POJO:

public class ChatMessage {

private Map<String, String> map = new HashMap<>();

@JsonAnyGetter
public Map<String, String> any(){
    return this.map;
}

public Map<String, String> getMap() {
    return this.map;
}

@JsonAnySetter
public void setMap(String key, String value) {
    map.put(key, value);
}

@Override
public String toString() {
    return "Map [map=" + map + "]";
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

if we try to change the objectname from message to json-message, how jackson-object mapper would identify and convert to object?

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.