我正在使用使用码头的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 -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
Notice响应标头中的charset=**utf-8**
,但我通过注释produces=MediaType.APPLICATION_JSON_UTF8_VALUE
将标头设置为值application/json;charset=UTF-8
。因此,Jetty(使用tomcat可以正常工作)在响应标头中小写了字符集。
为什么会有问题?有些人在我的端点上工作,并使用JSON Valiadtor进行验证(例如:https://jsonformatter.curiousconcept.com/)。
此验证器期望大写的字符集。 (请参见https://stackoverflow.com/a/48466826/3046582)。那我该怎么办?
请注意,字符集名称和语言标签均限于US-ASCII字符集,并且不区分大小写地进行匹配(请参见[RFC2978]第2.3节和[RFC5646]第2.1.1节)。
[W3 Org's example使用Content-Type: text/html; charset=utf-8
作为“典型”报头。