解决springboot responseentity<string>乱码问题
springboot responseentity<string>乱码
乱码
<200,{"result":"[{\"field\":\"name\",\"objectName\":\"driver\",\"defaultMessage\":\"åç§°ä¸èƒ½ä¸ºç©ºï¼\"},{\"field\":\"address\",\"objectName\":\"driver\",\"defaultMessage\":\"系统idä¸èƒ½ä¸ºç©ºï¼\"},{\"field\":\"authcode\",\"objectName\":\"driver\",\"defaultMessage\":\"认è¯ç ä¸èƒ½ä¸ºç©ºï¼\"}]"},{Connection=[keep-alive], Set-Cookie=[JSESSIONID=lUFZC2gIOg0eoUdfdmWW6KSYCP7aY8FErr6BBu9T; path=/], Content-Type=[text/plain], Content-Length=[302], X-Application-Context=[application:6060], Date=[Fri, 06 Aug 2021 01:11:25 GMT]}>
解决方法
try
{
String seqResult = new String(stringResponseEntity.getBody().getBytes("ISO8859-1"),"utf-8");
System.out.println(seqResult);//这就是UTF-8的啦
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}加 try catch是为了解决
Unhandled exception: java.io.UnsupportedEncodingException
解决乱码后
{"result":"[{\"field\":\"name\",\"objectName\":\"driver\",\"defaultMessage\":\"名称不能为空!\"},{\"field\":\"address\",\"objectName\":\"driver\",\"defaultMessage\":\"系统id不能为空!\"},{\"field\":\"authcode\",\"objectName\":\"driver\",\"defaultMessage\":\"认证码不能为空!\"}]"}
responseentity下载文件名中文乱码
SpringBoot下载文件,文件名带了中文出现乱码:

代码如下:
return ResponseEntity.ok()
.header("Content-disposition", "attachment;filename=" + fileName)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);解决办法
给文件名进行编码:
fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");问题解决!

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
- Springboot3 ResponseEntity 完全使用案例
- SpringBoot中ResponseEntity的使用方法举例详解
- 一文详解Spring中ResponseEntity包装器的使用
- SpringBoot的ResponseEntity类返回给前端具体讲解
- SpringBoot ResponseEntity标识Http响应方式
- springmvc @ResponseStatus和ResponseEntity的使用
- SpringMVC使用ResponseEntity实现文件上传下载
- 使用spring框架ResponseEntity实现文件下载
- Spring ResponseEntity的使用详解
相关文章
Spring框架JavaMailSender发送邮件工具类详解
这篇文章主要为大家详细介绍了Spring框架JavaMailSender发送邮件工具类,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2019-04-04
Java基础之Comparable与Comparator概述
这篇文章主要介绍了Java基础之Comparable与Comparator详解,文中有非常详细的代码示例,对正在学习java基础的小伙伴们有非常好的帮助,需要的朋友可以参考下2021-04-04
mybatis/mybatis-plus模糊查询语句特殊字符转义拦截器的实现
在开发中,我们通常会遇到这样的情况。用户在录入信息是录入了‘%’,而在查询时无法精确匹配‘%’。究其原因,‘%’是MySQL的关键字,如果我们想要精确匹配‘%’,那么需要对其进行转义,本文就详细的介绍一下2021-11-11


最新评论