Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What if the ResponseEntity returned by SpringMVC is garbled?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what to do when the ResponseEntity returned by SpringMVC is garbled. I hope you will get something after reading this article. Let's discuss it together.

Return ResponseEntity garbled problem

The foreground queries the relevant data from the data to the background, and the result is that the data returned by the background is garbled in Chinese, as shown in the following figure.

Reason

For the String type, if the encoding type is not clearly specified in the code, the transmitted data may not be encoded correctly in the background (my guess is that time is limited, and later I will follow the source code and look at the documentation to find out the real reason)

Solve

Change the String type to user-defined Pojo, such as User, or to more advanced data types such as List. Then serialize through @ ResponseBody to solve the Chinese garbled problem, as shown in the following figure.

ResponseEntity download file name garbled in Chinese

SpringBoot downloads files with garbled code in Chinese

The code is as follows:

Return ResponseEntity.ok () .header ("Content-disposition", "attachment;filename=" + fileName) .contentLength (file.length ()) .contentType (MediaType.parseMediaType ("application/octet-stream")) .body (resource); solution

Encode the file name:

FileName = new String (fileName.getBytes ("UTF-8"), "ISO-8859-1"); problem solving

After reading this article, I believe you have a certain understanding of "what to do about the garbled ResponseEntity returned by SpringMVC". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report