我使用的是使用jetty的Spring应用程序:
<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 -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注意到响应头中的charset=**utf-8**,但是我通过注释produces=MediaType.APPLICATION_JSON_UTF8_VALUE将标题设置为值application/json;charset=UTF-8。所以Jetty (使用tomcat一切都很好)将响应头中的字符集小写。
为什么这是个问题?一些人使用JSON对我的端点进行验证(比如:https://jsonformatter.curiousconcept.com/)。
此验证器期望字符集以大写字母表示。(见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");将解决这个问题。
发布于 2019-10-08 14:41:34
那么验证器就坏了。等级库要求不区分大小写。
请注意,字符集名称和语言标记都仅限于US字符集,并且不敏感地匹配大小写(参见RFC2978,第2.3节和RFC5646,第2.1.1节)。
W3 Org示例使用Content-Type: text/html; charset=utf-8作为“典型的”标头。
但如果问题是,为什么Jetty要把它小写呢?好吧,我决定在消息来源中四处搜寻,然后找到了字符集是消毒的的位置。
从那里,我们找到了HttpGenerator
如果系统属性"org.eclipse.jetty.http.HttpGenerator.STRICT“设置为true,则生成器将严格传递从方法和标头字段接收的准确字符串。否则,将使用快速的不区分大小写的字符串查找,这可能会改变某些方法/标头的大小写和空白。
https://stackoverflow.com/questions/58288166
复制相似问题