我对spring boot rest api特殊cs\u cs字符的编码有问题。
例子:
@ResponseBody
@RequestMapping("/api/test")
public String test(){
String specialCH = "ěščřžýáíéúů";
System.out.println(specialCH);
return specialCH;
}
2020-11-16 11:22:04.557 INFO 7400 --- [ main] cz.fry.chmi.ChmiApplication : Started ChmiApplication in 5.187 seconds (JVM running for 5.703)
2020-11-16 11:22:05.167 INFO 7400 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-11-16 11:22:05.168 INFO 7400 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-11-16 11:22:05.175 INFO 7400 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 7 ms
ěščřžýáíéúů
浏览器中的结果:
结果
应用程序属性:
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force=true
操作系统Windows101909。
ps:如果我创建文件test.html->
+ěššččřřžýáíéáý
浏览器中的结果:
结果
1条答案
按热度按时间xzabzqsa1#
我找到了解决办法
只需在spring mvc中强制utf-8编码返回字符串:
在
@RequestMapping
,使用:produces = MediaType.APPLICATION_JSON_VALUE + "; charset=utf-8"