为什么在这两种情况下unicode编码不同?

Why is unicode encoded differently in these two scenarios?

我写了这个最小的例子:

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");
}

为什么两个端点中的每一个 return 都有不同的结果?

更重要的是,根据 2019 年的标准和最佳实践,这两个结果中哪一个是严格正确的?

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"}

在第一种情况下 Spring 使用您的默认字符集 (ISO-8859-1),而在第二种情况下,当 Spring 负责 JSON 序列化时,UTF-8 .

来自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).

您可以使用 produces = MediaType.APPLICATION_JSON_UTF8_VALUE 或通过配置 OS.

明确指定 UTF-8