gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:08:58 25 4
gpt4 key购买 nike

我写了这个最小的例子:

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

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

enter image description here

更重要的是,根据 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。

来自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 themaximum 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 或通过配置操作系统来明确指定 UTF-8。

关于java - 为什么在这两种情况下unicode编码不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58908651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com