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

Java 为什么在这两种情况下unicode编码不同?,java,spring-mvc,unicode,jackson,unicode-escapes,Java,Spring Mvc,Unicode,Jackson,Unicode Escapes,我写了这个最小的例子: 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; } } @Request

我写了这个最小的例子:

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");
}
为什么两个端点都返回不同的结果

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

标题

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

从:

JSON文本应采用UTF-8、UTF-16或UTF-32编码。默认值
编码是UTF-8,而以UTF-8编码的JSON文本是
可互操作,即用户将成功读取这些文件 实现的最大数量;有许多实现
无法成功读取其他编码(如
UTF-16和UTF-32)


您可以使用
products=MediaType.APPLICATION\u JSON\u UTF8\u VALUE
或通过配置操作系统显式指定UTF-8。

看起来您的
unicodeTest1()
根本没有被编码,因为否则您将得到一个JSON字符串值,而不是您拥有的JSON对象。(同样,对于编码问题,请发布十六进制转储,而不是带有解释字符的终端屏幕截图。)能否显示
内容-*
响应标题?(
curl-i
)已更新……。看起来我需要在第一个变量中使用
products=MediaType.APPLICATION\u JSON\u UTF8\u VALUE
,使其与第二个变量相同。