0
点赞
收藏
分享

微信扫一扫

springboot返回前端中文乱码

婉殇成长笔记 2022-03-30 阅读 118
java后端

解决方式:

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

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));
举报

相关推荐

0 条评论