The JSON returned by SpringBoot is escaped with a slash

My framework will add a wrapping result to the return type, but I left a gap, that is, the return String will not be wrapped. I recently wrote an interface. Because it is returned by the previous interface, the return result cannot be changed, so the String I return will not be wrapped. The code is as follows:

    @RequestMapping(value = "webSwitch")
    public Object webSwitch() {
        JsonObject result = new JsonObject();
        result.addProperty("success", 200);
        result.addProperty("message", "ok");
        result.addProperty("openWeb", showWebVO.getOpenWeb());

        return result.toString();
    }

The result returned by the test result has a backslash and cannot be parsed by json. The results are as follows:

"{\"success\":200,\"message\":\"ok\",\"openWeb\":true,\"webUrl\":null}"

Reason analysis: Because I put CustomGsonMessageConvertor first in my framework, I use it to write response by default. As a result, when gson writes a string, it finds that there are special characters in json format, and it will be escaped.

After figuring out the reason, add produces = "text/plain" to be written back by StringhttpMessageConvertor and it's fine.

Guess you like

Origin blog.csdn.net/CharlesYooSky/article/details/109477983