[在邮递员中,我得到了正确的json响应,但是当我通过restTemplate访问服务时,我得到了汉字

问题描述 投票:0回答:2

代码:

RestTemplate template=new RestTemplate();  
HttpHeaders headers=new HttpHeaders();                                                                             
headers.setContentType(MediaType.APPLICATION_JSON);                               
HttpEntity<?> httpEntity=new HttpEntity<>(body,headers);     
ResponseEntity<String> postForEntity = template.postForEntity("url", httpEntity, String.class);

Json回复:

笀∀琀爀愀渀猀愀挀琀碗漀渀䤀䐀∀㨀∀㄀娀㜀㌀㐀䔀㤀㈀㈀㘀㜀㈀㌀㔀㠀㘀㤀㔀∀Ⰰ∀猀琀愀琀甀猀∀㨀Ⰰ ∀爀攀焀吀猀∀㨀∀㈀㈀ⴀ㐀ⴀ㈀∀Ⰰ∀愀搀搀爀伥漀碗渀琀䰀碗猀琀∀㨀嬀笀∀爀攀焀䤀搀∀㨀∀㄀娀㜀㌀㐀䔀㤀㈀㈀㘀㜀㈀㌀㔀㠀㘀㤀㔀∀Ⰰ∀爀攀猀碗䌀漀洀∀㨀∀唀∀Ⰰ∀愀搀搀爀伥渀琀吀欀渀∀㨀∀∀纪崀纪

通过使用此for循环,我可以获得正确的json响应:

for(int i=0; i< response.length(); i++) {
     encoded += ((char) ((int)response.charAt(i) / 256 ));    
}

邮递员标题:

Request Headers: 
Accept:Application/Json
Content-type:Application/Json

Response Headers:
Content-Type →application/json; charset=utf-8
Date →Thu, 04 Jun 2020 15:41:49 GMT
Server →Server Name
Transfer-Encoding →chunked

但是我想不使用此for循环就得到响应。

java resttemplate
2个回答
-2
投票

也许您需要传递“ Accept-Language”标头。邮递员可能会将默认标头值发送为“ en-US”


-2
投票

问题是编码。发送请求时,请确保添加标题

Content-type:Application/Json; charset=utf-8

不只是

Content-type:Application/Json
© www.soinside.com 2019 - 2024. All rights reserved.