gpt4 book ai didi

java - Jetty 字符集 utf-8 与 字符集 UTF-8

转载 作者:行者123 更新时间:2023-12-02 09:26:51 35 4
gpt4 key购买 nike

我正在使用使用jetty的Spring-Web应用程序:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

假设此 http 端点:

@RestController
public class ExampleController {

@GetMapping(value = "/example", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ExampleResponse example() {
return new ExampleResponse();
}

public static class ExampleResponse {
private String dummy = "example";

public String getDummy() {
return dummy;
}
}
}

并针对端点进行curl并检查 header curl -v localhost:8080/example:

* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /example HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Tue, 08 Oct 2019 13:52:10 GMT
< Content-Type: application/json;charset=utf-8
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact

注意响应 header 中的 charset=**utf-8**,但我通过注释设置 header products=MediaType.APPLICATION_JSON_UTF8_VALUE 到值 application/json;charset=UTF-8。因此 Jetty(使用 tomcat 一切正常)将响应 header 中的字符集小写。

为什么这是一个问题?有些人针对我的端点工作并使用 JSON Valiadtor 进行验证(例如: https://jsonformatter.curiousconcept.com/ )。

此 validator 需要大写的字符集。 (参见https://stackoverflow.com/a/48466826/3046582)。那么我该怎么办呢?

更新:

就像@Kayaman所说的System.setProperty("org.eclipse.jetty.http.HttpGenerator.STRICT", "true");在Spring应用程序运行之前将解决这个问题。

我还找到了一个解决方法:MimeTypes.CACHE.remove("application/json;charset=utf-8"); 将解决这个问题。

最佳答案

那么 validator 就坏了。 spec要求不区分大小写。

Note that both character set names and language tags are restricted tothe US-ASCII character set, and are matched case-insensitively (see[RFC2978], Section 2.3 and [RFC5646], Section 2.1.1).

还有W3 Org's example使用Content-Type: text/html; charset=utf-8作为“典型” header 。

但如果问题是,为什么 Jetty 将其小写?好吧,我决定在源代码中隐藏起来,并找到了 charset is sanitized 的位置。 .

从那里,前往HttpGenerator我们发现

If the system property "org.eclipse.jetty.http.HttpGenerator.STRICT"is set to true, then the generator will strictly pass on the exactstrings received from methods and header fields. Otherwise a fastcase insensitive string lookup is used that may alter the case andwhite space of some methods/headers

关于java - Jetty 字符集 utf-8 与 字符集 UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58288166/

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