Java Spring: Web Service REST producing double backslash at JSON
java, json, rest, spring, spring-mvc
Solution
That is the expected behavior in JSON. `\` needs to be escaped. See the specification, here.
All Unicode characters may be placed within the quotation marks, except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).
Problem
So i have this String e.g. ``` "B\z@b@s@rB{FpMgBrD~DK|" ``` But Spring is producing this at json: ``` "B\\z@b@s@rB{FpMgBrD~DK|" ``` And it's the same with any string that has a backslash `\`. Replacing the character before generating the json is useless, since the String is correct before the json is generated. Every client that consumes the service could .replace("\\","\\\\") the json, but I wonder if there will be a cleaner way to solve the problema and at server side. Here is the code for the WS: ``` @RequestMapping(value = "/rest/sinc/{ms}", method = RequestMethod.GET, produces="application/json;charset=UTF-8") @ResponseBody public String sincronizar(@PathVariable("ms") Long ms) { return sincService.getSinc(ms).toString(); } ``` I have tried other ways to generate the json with libraries like Gson but the result is the same.