3

I have written this minimal example:

public static class X {
    private String x;

    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }

    public X(String x) {
        super();
        this.x = x;
    }

}
@RequestMapping(value = "/unicode.test1", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String unicodeTest1() {
    return "{\"x\":\"X\u00ADX\"}";
}
@RequestMapping(value = "/unicode.test2", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public X unicodeTest2() {
    return new X("X\u00ADX");
}

Why does each of the two endpoints return a different result?

enter image description here

More importantly, which is of the two results is strictly correct as of 2019 standards and best practices?

HEADERS

C:\>curl -i "http://localhost/rets_api/unicode.test1"
HTTP/1.1 200
Content-Type: application/json;charset=ISO-8859-1
Content-Length: 11
Date: Mon, 18 Nov 2019 06:24:01 GMT

{"x":"X¡X"}
C:\>curl -i "http://localhost/rets_api/unicode.test2"
HTTP/1.1 200
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Mon, 18 Nov 2019 06:24:05 GMT

{"x":"X­X"}
Alex R
  • 11,364
  • 15
  • 100
  • 180

1 Answers1

2

In the first case Spring uses your default charset (ISO-8859-1), while in the second, when Spring is responsible for JSON serialization, UTF-8.

From the RFC:

JSON text SHALL be encoded in UTF-8, UTF-16, or UTF-32. The default
encoding is UTF-8, and JSON texts that are encoded in UTF-8 are
interoperable in the sense that they will be read successfully by the maximum number of implementations; there are many implementations
that cannot successfully read texts in other encodings (such as
UTF-16 and UTF-32).

You can explicitly specify UTF-8 using produces = MediaType.APPLICATION_JSON_UTF8_VALUE or by configuring your OS.

Community
  • 1
  • 1
Alex Filatov
  • 4,768
  • 2
  • 16
  • 10