解决后端返回json数据给前端显示乱码问题
1.问题:问题代码:出问题的问题代码:package com.example.demo;import com.example.Bean.Student;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestCon...
·
1.问题:
问题代码:

出问题的问题代码:
package com.example.demo;
import com.example.Bean.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class IndexController02 {
@RequestMapping("/index02")
public Student index02(){
System.out.println("***********************进入后台02success************");
Student stu = new Student();
stu.setId(1);
stu.setStuName("张三");
stu.setCreateTime(new Date());
stu.setRemarks("备注信息,不应该返回!");
return stu;
}
}
2.修改后的效果:
修改后的代码:
package com.example.demo;
import com.example.Bean.Student;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class IndexController02 {
@RequestMapping(value = "/index02",produces = {"application/json;charset=UTF-8"})
public Student index02(){
System.out.println("***********************进入后台02success************");
Student stu = new Student();
stu.setId(1);
stu.setStuName("张三");
stu.setCreateTime(new Date());
stu.setRemarks("备注信息,不应该返回!");
return stu;
}
}
解决问题的方式:
在controller类中,调用的方法上加这样一条,可以把json格式改为UTF-8,问题就解决了
@RequestMapping(value=“xxx”,produces = {“application/json;charset=UTF-8”})
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)