软件编程
位置:首页>> 软件编程>> java编程>> springboot返回前端中文乱码的解决

springboot返回前端中文乱码的解决

作者:雄二说  发布时间:2023-08-09 11:49:12 

标签:springboot,返回前端,乱码

尝试了各种防止中文乱码的方式,但是还是乱码;最后还是细节问题导致;

解决方式:

以及俩种方式是百度的,我的问题不是这俩块

1.在requestMapping 中添加 produces


@RequestMapping(
value = "/login",
produces = "application/json;charset=utf-8",
method = RequestMethod.POST
)

2.在application.yml 中添加配置


spring:
 http:
   encoding:
     force: true
     charset: utf-8
     enabled: true

3.解决单个字符串乱码


String name = new String(user.getName().getBytes("ISO-8859-1"),"UTF-8");

我的乱码问题的解决方式

接口添加 @ResponseBody 是返回对象到前端就会展示成json格式,但有的时候会乱码;
比如下面的写法


User user = new User();//假装有数据
JSONObject output = new JSONObject();
output.put("userInfo": user);

user添加到JSONObject中 user里面的中文就会乱码;

返回前端的数据还是先将对象转成 JSON然后在 return


User user = new User();//假装有数据
JSONObject output = new JSONObject();
output.put("userInfo": JSON.toJSON(user));

来源:https://blog.csdn.net/qq_34158598/article/details/86092713

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com