Java Class parsed to JSON does not output correct Json Object

Viewed 27
  • I use Java (with Quarkus) for the backend.
  • Vue3 for the frontend.
  • API and Websockets to transition data between the two.

I am building a chat message application. So far I was able to send a message inside the backend under a JSON format, do stuff in Java with this message (which holds the message inside a MessageJson class that I have created, hence the data type is now type of MessageJson), transform it into a String, then and send it back to the frontend. I wish to transform the String in the frontend, into a Json.

Here is my function which send the message back to the front (using Websockets):

public MessageResponseForm processBackend(MessageJson messageJson) {
        MessageResponseForm messageResponseForm = new MessageResponseForm();
        messages(messageJson);
        webSocketConfig.sendMessage("U001", messageJson.toString());
        return messageResponseForm;
    }

The frontend retrieve it without issue, in the String format. But I need it back into Json format.

I use the function JSON.parse() in Vue.js but it gives me this: enter image description here

Which is normal in my opinion, because I should receive something like this from the backend:

"{"toRecipient":"U002","fromSender":"U00555","messageContent":"ewww555"}" .....

But instead I got this:

"MessageJson(toRecipient=U002, fromSender=U00555, messageContent=ewww555, dateCreated=2022-9-23 14:15:57, companyName=test)"

My Class MessageJson does not seems to keep the correct Json format. How to prevent this? Do I need to use the @Produces annotations in Quarkus? If yes, where exactly?

I found no other method related to ToString()...

EDIT: Do I need to use the dependency GSON to do the job, or can I do this natively?

0 Answers
Related