Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.decodeValue Decode Exception

Tags:

java

json

I want to make this java code works:

RequestManager rm = Json.decodeValue(request.getBodyAsString(), RequestManager.class);

But i have this error:

io.vertx.core.json.DecodeException: Failed to decode:No suitable constructor found for type [simple type, class RequestManager]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?) at [Source: {"messageId":"fsdfsdf"}; line: 1, column: 2]

And here the code of my class :

public class RequestManager {
    private String messageId;
    private String messageContent;

    public RequestManager(String messageId, String messageContent) {
        this.messageId = messageId;
        this.messageContent = messageContent;
    }

    public String getMessageId() {
        return messageId;
    }

    public String getMessageContent() {
        return messageContent;
    }
}

I really don't know why it's not working and there is only few topics about it, but they were irrelevant.

Someone can help ?

EDIT--

I know have the RequestManager class like this:

public class RequestManager {
    private String messageId;
    private String messageContent;

    public RequestManager(String messageId, String messageContent) {
        this.messageId = messageId;
        this.messageContent = messageContent + "check";
    }

    public RequestManager() {

    }

    public String getMessageId() {
        return messageId;
    }

    public String getMessageContent() {
        return messageContent;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    public void setMessageContent(String messageContent) {
        this.messageContent = messageContent;
    }
}

But know when i try to print the fields of my RequestManager object created with the JSON.decodeValue it's return me null. I've already done that in the past and had the same error. I think it's because the empty constructor is used instead. I still don't really understand....

EDIT--2

I have tried to change my class again, here it is:

@JsonIgnoreProperties(ignoreUnknown=true)
public class RequestManager {
    @JsonProperty("messageId") private String messageId;
    @JsonProperty("messageContent") private String messageContent;

    @JsonCreator
    public RequestManager(@JsonProperty("messageId") String messageId, @JsonProperty("messageContent") String messageContent) {
        this.messageId = messageId;
        this.messageContent = messageContent;
        System.out.println("This constructor is used.");
    }

    public RequestManager() {

    }

    public String getMessageId() {
        return messageId;
    }

    public String getMessageContent() {
        return messageContent;
    }

    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }

    public void setMessageContent(String messageContent) {
        this.messageContent = messageContent;
    }
}

And this is in my main : final RequestManager rm = Json.decodeValue("{\"messageId\":\"themessage\"}", RequestManager.class); System.out.println(rm.getMessageContent());

"{\"messageId\":\"themessage\"}" = the JSON format, i'm sure of it because decodeValue would return a Decode Exception if it wasn't.

Now the field is "nullcheck" when i print it. So it means that the constructor is well used but the fields are nulls. Where am i doint it wrong ?

like image 579
morpheus21 Avatar asked Jul 07 '26 09:07

morpheus21


1 Answers

You could try to have an empty constructor.

like image 70
Louis F. Avatar answered Jul 09 '26 21:07

Louis F.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!