gpt4 book ai didi

java - 响应字符集不正确

转载 作者:行者123 更新时间:2023-12-01 17:27:12 24 4
gpt4 key购买 nike

我编写了一个方法,该方法应该以 utf-8 的字符串类型返回有效的正文,但在发出请求后,我得到了 utf-16 的响应。postman 中的内容类型 header 是 application/json。我在 stackoverflow 上搜索了任何可能的方法来解决它,但任何解决方案都没有帮助我,包括 this话题(。我怎样才能改变它以获得有效的 body ?谢谢!

Headers, that i've got on response:
{Date=[Mon, 13 Apr 2020 19:13:38 GMT], Content-Type=[application/json; charset=utf-16], Server=[Kestrel], Transfer-Encoding=[chunked]}
    @PostMapping("/updateValues")
public String getUpdateValuesRequest(@RequestParam(value = "secret") String secret,
@RequestHeader HttpHeaders requestHeaders,
@RequestBody String requestBody){
return getUpdateValuesResponse(secret, requestHeaders, requestBody);
}

public String getUpdateValuesResponse(String secret, HttpHeaders requestHeaders, String requestBody) {
String updateValuesUrl = "http://myservice.com/chunk/updateValues?secret={secret}";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
HttpEntity<String> entity = new HttpEntity<String>(requestBody, requestHeaders);
ResponseEntity<String> response = restTemplate.exchange(updateValuesUrl, HttpMethod.POST, entity, String.class, secret);
if (response.getStatusCode() == HttpStatus.OK) {
logger.info("Request Successful.");
} else {
logger.info("Request Failed");
}
logger.info(response.getHeaders().toString());
logger.info(response.getStatusCode().toString());
logger.info("StatusCodeValue: " + response.getStatusCodeValue());
logger.info(response.getBody());
logger.info(response.getClass().toString());
return response.getBody();
}

最佳答案

通过为 stringHttpMessageConverter 设置 setWriteAcceptCharset(false) 解决了该问题。

关于java - 响应字符集不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61195269/

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