【发布时间】:2021-08-13 17:52:20
【问题描述】:
在我的 REST 控制器中,我使用
@PostMapping、@GetMapping等,没有任何其他规范。
因此默认值必须是JSON,例如@GetMapping。也没有指定字符编码,我假设它必须是UTF-8,我在文档中找不到默认字符编码。
但是在我的测试中我使用MockMvc。
当我发出POST 请求时,它看起来像这样:
public static MvcResult performPost(MockMvc mockMvc, String endpoint, String payload, ResultMatcher status) throws Exception {
MvcResult mvcResult = mockMvc.perform(
post(endpoint)
.content(payload)
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andDo(print())
.andExpect(status)
.andReturn();
return mvcResult;
}
问题:.andDo(print()) 部分似乎没有使用 UTF-8。如何解决这个问题?在我的 NetBeans IDE 的控制台中,某些字符(例如德语 'ü')未正确打印。它看起来像(见正文):
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/json", X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
Content type = application/json
Body = {"Tür"}
Forwarded URL = null
Redirected URL = null
Cookies = []
问题:
当我的方法返回MvcResult时,我可以这样做:
MockHttpServletResponse response = mvcResult.getResponse();
ObjectMapper objectMapper = new ObjectMapper();
String contentAsString = response.getContentAsString(StandardCharsets.UTF_8);
我发现,我必须使用StandardCharset.UTF_8 来获取正确的字符,例如'ü'。
但是为什么MockHttpServletResponse response 是characterEncoding ISO-8859-1? ISO-8859-1来自哪里,这一套在哪里?可以改成UTF-8吗?
当我改为尝试时:
String contentAsString = response.getContentAsString(StandardCharsets.ISO_8859_1);
我没有得到德语 'ü',字符串值是 "Tür"。虽然在ISO_8859_1 中根据https://en.wikipedia.org/wiki/ISO/IEC_8859-1 字符'ü' 在代码页布局表中。
【问题讨论】:
标签: java spring spring-boot mockmvc