English 中文(简体)
部分中文
原标题:Rest Template Returns Response in Garbled Characters Some Sort of Chinese
I am using rest template to call API, when I hit the API using rest template it returns some garbled characters like Chinese. ResponseEntity response = restTemplate.exchange(url, method, entity, String.class); When I hit the same API using Unirest or Postman, it returns response in JSON. I searched and tried to figure it out but there is a constraint that I can t do changes in the code either I do change in application.yml or pom.xml but can not change at the code level. <200,笀∀爀攀焀甀攀猀琀䐀愀琀攀吀椀洀攀∀㨀∀㈀ ㈀㐀ⴀ 㜀ⴀ㈀㌀吀㄀ 㨀㈀㄀㨀㄀㠀⸀㜀 㜀㘀㐀㄀㐀⬀ 㔀㨀  ∀Ⰰ∀氀漀最䤀搀∀㨀∀㈀㜀  ㄀㜀昀戀ⴀ 攀㤀戀ⴀ㐀㐀戀搀ⴀ㤀搀 㠀ⴀ㔀㔀㄀㄀㌀㔀搀㜀搀 戀昀∀Ⰰ∀挀漀搀攀∀㨀∀䴀匀䜀ⴀ      ∀Ⰰ∀猀攀瘀攀爀椀琀礀∀㨀∀匀甀挀挀攀猀猀∀Ⰰ∀挀漀渀琀攀渀琀∀㨀∀攀礀䨀栀戀䜀挀椀伀椀䨀䤀唀稀䤀㄀一椀䤀猀䤀渀刀㔀挀䌀䤀㘀䤀欀瀀堀嘀䌀䨀㤀⸀攀礀䨀㄀戀洀氀砀搀圀嘀昀戀洀䘀琀娀匀䤀㘀䤀洀䤀㐀娀䐀䔀㈀䴀圀䔀礀䰀圀夀㈀夀樀挀琀一䐀䴀砀一礀㄀栀夀樀䔀 䰀吀欀㌀䴀䐀最稀伀吀䴀㐀娀圀䔀㐀䴀椀䤀猀䤀洀㔀椀娀椀䤀㘀䴀吀挀礀䴀吀挀砀䴀樀䄀㌀伀䌀眀椀娀堀栀眀䤀樀漀砀一稀䤀砀一稀䔀㄀一樀挀㐀䰀䌀䨀瀀夀堀儀椀伀樀䔀㌀䴀樀䔀㌀䴀吀䤀眀一稀栀㤀⸀挀䤀䄀攀䈀砀ⴀ瘀爀吀娀匀㠀㤀㠀甀㈀䄀㔀娀䰀㔀䴀倀䔀愀 爀欀伀儀瘀堀漀ⴀ樀ⴀ洀䤀礀唀  ∀Ⰰ∀攀爀爀漀爀∀㨀渀甀氀氀紀,{Content-Length=[780], Content-Type=[application/json; charset=utf-16], Server=[Microsoft-IIS/10.0], Date=[Tue, 23 Jul 2024 05:21:18 GMT]}> This is the response I get. And I want response in the below format. <200,{requestDateTime=2024-07-23T10:23:45.9429614+05:00, logId=e7dc9988-2a2a780b54c6, code=200, severity=Success, content=eyJNiIsInR5cCI6IkpXVCJhbGciOiJIUzI19.eyJ1bSI6ImI4ZDE2MWEyMTIyMjV9LWY2mlxdWVfbmFtZYjctNDMcyMTcxMjIyNSwiZXhwIjoxNzIxNzE1ODI1LCJpYXQiOjE3MjxNy1hYjE0LTk3MDgzOTM4ZWE4MiIsIm5iZiI6MTE3.yGUCZ83YVYw7XOuXrIl79ONCYVuxKS5MbtB_XxG9wyE, error=null},{Content-Length=[780], Content-Type=[application/json; charset=utf-16], Server=[Microsoft-IIS/10.0], Date=[Tue, 23 Jul 2024 05:23:45 GMT]}> I receive the above expected response when I change the return to Object.class but as I said earlier I can t make that change. I have also made the following changes in my application.yml file: spring.mandatoryFileEncoding=UTF-8 spring.http.encoding.charset=UTF-8 spring.http.encoding.enabled=true spring.http.encoding.force=true And following changes in pom.xml: UTF-8 UTF-8 But nothing works for me. Kindly suggest me is there any possible solution to get the response in JSON format without making changes in the code. TIA.
问题回答
Your server is generating text in the UTF-16 charset, as evidenced by the header Content-Type: application/json; charset=utf-16. Unfortunately, UTF-16 is by itself an ambiguous charset: there are two variants, UTF-16-BE ("big endian") and UTF-16-LE ("little endian"). Normally, if only "UTF-16" is specified, the first two bytes of the response should be a byte order mark, either FF FE or FE FF, signifying the endian used to encode the message. However, your server is not sending the byte order mark. Therefore, Spring ends up having to guess which endianness is being used - and Spring is guessing wrong. I can demonstrate that the text is being received with the wrong endianness with a short Python script that "ungarbles" the message you received: r = r 笀∀爀攀焀甀攀猀琀䐀愀琀攀吀椀洀攀∀㨀∀㈀ ㈀㐀ⴀ 㜀ⴀ㈀㌀吀㄀ 㨀㈀㄀㨀㄀㠀⸀㜀 㜀㘀㐀㄀㐀⬀ 㔀㨀  ∀Ⰰ∀氀漀最䤀搀∀㨀∀㈀㜀  ㄀㜀昀戀ⴀ 攀㤀戀ⴀ㐀㐀戀搀ⴀ㤀搀 㠀ⴀ㔀㔀㄀㄀㌀㔀搀㜀搀 戀昀∀Ⰰ∀挀漀搀攀∀㨀∀䴀匀䜀ⴀ      ∀Ⰰ∀猀攀瘀攀爀椀琀礀∀㨀∀匀甀挀挀攀猀猀∀Ⰰ∀挀漀渀琀攀渀琀∀㨀∀攀礀䨀栀戀䜀挀椀伀椀䨀䤀唀稀䤀㄀一椀䤀猀䤀渀刀㔀挀䌀䤀㘀䤀欀瀀堀嘀䌀䨀㤀⸀攀礀䨀㄀戀洀氀砀搀圀嘀昀戀洀䘀琀娀匀䤀㘀䤀洀䤀㐀娀䐀䔀㈀䴀圀䔀礀䰀圀夀㈀夀樀挀琀一䐀䴀砀一礀㄀栀夀樀䔀 䰀吀欀㌀䴀䐀最稀伀吀䴀㐀娀圀䔀㐀䴀椀䤀猀䤀洀㔀椀娀椀䤀㘀䴀吀挀礀䴀吀挀砀䴀樀䄀㌀伀䌀眀椀娀堀栀眀䤀樀漀砀一稀䤀砀一稀䔀㄀一樀挀㐀䰀䌀䨀瀀夀堀儀椀伀樀䔀㌀䴀樀䔀㌀䴀吀䤀眀一稀栀㤀⸀挀䤀䄀攀䈀砀ⴀ瘀爀吀娀匀㠀㤀㠀甀㈀䄀㔀娀䰀㔀䴀倀䔀愀 爀欀伀儀瘀堀漀ⴀ樀ⴀ洀䤀礀唀  ∀Ⰰ∀攀爀爀漀爀∀㨀渀甀氀氀紀 print(r.encode("utf-16-le").decode("utf-16-be")) This prints {"requestDateTime":"2024-07-23T10:21:18.7076414+05:00","logId":"270017fb-0e9b-44bd-9d08-551135d7d0bf","code":"MSG-000000","severity":"Success","content":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6ImI4ZDE2MWEyLWY2YjctNDMxNy1hYjE0LTk3MDgzOTM4ZWE4MiIsIm5iZiI6MTcyMTcxMjA3OCwiZXhwIjoxNzIxNzE1Njc4LCJpYXQiOjE3MjE3MTIwNzh9.cIAeBx-vrTZS898u2A5ZL5MPEa0rkOQvXo-j-mIyU00","error":null}, which is a perfectly reasonable JSON response. Unfortunately, in this situation it s your server that is not sending proper responses, and should be fixed (if possible). The server should either: Send a UTF-16 response with a proper byte order mark to indicate the endian used Set the charset to either UTF-16-LE or UTF-16-BE to explicitly indicate the endian used Just use UTF-8 and avoid this mess entirely (recommended) I am not sure it s easy to fix Spring to use the "correct" encoding, unfortunately, as it is your server that is in violation of the relevant specifications.




相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签