Skip to content
Advertisement

Message contains escape character in the message sent to MQ and cause exception when converting to JSON

I have a method:

JavaScript

and my DTO’s toString():

JavaScript

I realise when the other application received the MQ message (using Spring Boot with JMS), the escape char appeared, causing errors. I tried to do replaceAll("\\", "") but it couldnt find anything to replace. How can I get rid of the in the message sent to the MQ?

Advertisement

Answer

The clean way to handle this is to use a proper JSON library to create the JSON string. For example, using the org.json library (javadoc).

JavaScript

This will escape the values in propA etcetera if this is necessary. The result will be well-formed JSON.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement