乱码问题
spring boot 中乱码有两种情况,一种配置文件中引入的变量乱码,一种是程序处理中文的乱码。
具体意思看代码, 这里 title 是properties配置文件 引入的中文,初次使用一般是乱码。
这里“我” 是代码写的, 参数 name 是请求时传入中文。 默认这两个不会乱码。
@Value("${com.neo.title}")
private String title;
@GetMapping("/")
public Map<String,String> index(@RequestParam(name = "name", defaultValue="world") String para) {
Map<String,String> ret = new HashMap<>();
ret.put("title","hello"+para+title);
ret.put("name","我");
return ret;
}
配置文件乱码解决
参照网上方案一般能解决了,方案如下:
打开Settings>Editor>File Encodings ,
将Properties Files (*.properties)
下的Default encoding for properties files
设置为UTF-8
,将Transparent native-to-ascii conversion
前的勾选上。
如果还不能解决,尝试在配置文件回车,修改等改动文件,或者删除文件重新新建。
如果还不能解决,请引用文章说的方式,用yml 格式的配置文件。
程序内容乱码,一般在配置文件里配置下面参数可以解决。
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8