3

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 ?

1

2 Answers 2

1

You could try to have an empty constructor.

Sign up to request clarification or add additional context in comments.

Comments

0

It's because you have your own constructor, and JSON doesn't know what values should be passed into it.

There is documentation on their GitHub page explaining how to set up a data object that you expect to be given to you as JSON and converted to Java.

https://github.com/vert-x3/vertx-codegen#data-objects

As per the example that you linked to: http://vertx.io/blog/some-rest-with-vert-x/, notice how they explicitly provide a constructor that takes no arguments, and public setter methods

Whisky()
setName(String name)
setOrigin(String origin)

The alternative is to provide annotations: https://github.com/FasterXML/jackson-annotations. You can choose how to do it, using annotation if you want, or using a bean class (getters and setters). Annotation has the advantage that you can say things like "ignore this value when you convert to JSON", etc. You can be more explicit with annotation. I would recommend picking one and staying with it. Consistency becomes important as your projects grow.

2 Comments

Thank you for your answer, my code is strongly inspired from this one, vertx.io/blog/some-rest-with-vert-x and i don't see any use of a anoted class. Is this a bad exemple ?
i see i will try to use the annotation as well but i've never used it.

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.