Jetty 字符集 utf-8 与字符集 UTF-8
Jetty charset utf-8 vs charset UTF-8
我正在使用 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;
}
}
}
并卷曲端点并检查 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**
,但我通过注解produces=MediaType.APPLICATION_JSON_UTF8_VALUE
将header设置为值 application/json;charset=UTF-8
。因此 Jetty(使用 tomcat 一切正常)将响应 header 中的字符集小写。
为什么这是个问题?有些人针对我的端点进行工作,并使用 JSON Valiadtor(例如:https://jsonformatter.curiousconcept.com/)对其进行验证。
此验证器需要大写字符集。 (参见 )。那么我能做些什么呢?
更新:
就像@Kayaman 说的那样System.setProperty("org.eclipse.jetty.http.HttpGenerator.STRICT", "true");
bevor Spring-Application 运行 会解决这个问题。
我还找到了解决方法:MimeTypes.CACHE.remove("application/json;charset=utf-8");
会解决这个问题。
然后那个验证器坏了。 spec 要求不区分大小写。
Note that both character set names and language tags are restricted to
the 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 exact
strings received from methods and header fields. Otherwise a fast
case insensitive string lookup is used that may alter the case and
white space of some methods/headers
我正在使用 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;
}
}
}
并卷曲端点并检查 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**
,但我通过注解produces=MediaType.APPLICATION_JSON_UTF8_VALUE
将header设置为值 application/json;charset=UTF-8
。因此 Jetty(使用 tomcat 一切正常)将响应 header 中的字符集小写。
为什么这是个问题?有些人针对我的端点进行工作,并使用 JSON Valiadtor(例如:https://jsonformatter.curiousconcept.com/)对其进行验证。
此验证器需要大写字符集。 (参见
更新:
就像@Kayaman 说的那样System.setProperty("org.eclipse.jetty.http.HttpGenerator.STRICT", "true");
bevor Spring-Application 运行 会解决这个问题。
我还找到了解决方法:MimeTypes.CACHE.remove("application/json;charset=utf-8");
会解决这个问题。
然后那个验证器坏了。 spec 要求不区分大小写。
Note that both character set names and language tags are restricted to the 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 exact strings received from methods and header fields. Otherwise a fast case insensitive string lookup is used that may alter the case and white space of some methods/headers