嘗試了各種防止中文亂碼的方式,但是還是亂碼;最后還是細(xì)節(jié)問(wèn)題導(dǎo)致;
解決方式:
以及倆種方式是百度的,我的問(wèn)題不是這倆塊
1.在requestMapping 中添加 produces
1
2
3
4
5
|
@RequestMapping ( value = "/login" , produces = "application/json;charset=utf-8" , method = RequestMethod.POST ) |
2.在application.yml 中添加配置
1
2
3
4
5
6
|
spring: http: encoding: force: true charset: utf-8 enabled: true |
3.解決單個(gè)字符串亂碼
1
|
String name = new String(user.getName().getBytes( "ISO-8859-1" ), "UTF-8" ); |
我的亂碼問(wèn)題的解決方式
接口添加 @ResponseBody 是返回對(duì)象到前端就會(huì)展示成json格式,但有的時(shí)候會(huì)亂碼;
比如下面的寫法
1
2
3
|
User user = new User(); //假裝有數(shù)據(jù) JSONObject output = new JSONObject(); output.put( "userInfo" : user); |
user添加到JSONObject中 user里面的中文就會(huì)亂碼;
返回前端的數(shù)據(jù)還是先將對(duì)象轉(zhuǎn)成 JSON然后在 return
1
2
3
|
User user = new User(); //假裝有數(shù)據(jù) JSONObject output = new JSONObject(); output.put( "userInfo" : JSON.toJSON(user)); |
到此這篇關(guān)于springboot返回前端中文亂碼的解決方法的文章就介紹到這了,更多相關(guān)springboot返回前端中文亂碼內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_34158598/article/details/86092713