概述
这里主要想讲一下关于key为中文时的情况,关于基础yaml语法就自行百度。
先说结论:
Yaml语法中的key不要用中文!!!
Yaml语法中的key不要用中文!!!
Yaml语法中的key不要用中文!!!
其实也不是完全不能为中文,当JavaBean的属性的变量名是中文的时候就可以使用,
但是yaml对中文key好像有各种各样的的bug,比如当JavaBean有多个中文变量名的时候,一个中文key,就会赋值给全部的中文变量。
反正就不要使用中文的key了,感觉中文就是有一些奇怪的问题!!!
以下举例:
JavaBean:
@Component
@ConfigurationProperties(prefix = "person")
@Data // Lombok中的注解,不知道的百度一下
public class Person {
private Map<String, Object> score;
}
Controller:
@RestController
public class TestController {
@Autowired
Person person;
@RequestMapping("/hello")
public Person hello(){ // 用于把person以json字符串形式返回 显示结果
return person;
}
}
key为中文的情况:
yaml文件:
person:
score:
语文: 90
数学: 95
结果:
Description:
Failed to bind properties under 'person.score' to java.util.Map<java.lang.String, java.lang.Object>:
Reason: No converter found capable of converting from type [java.lang.Integer] to type [java.util.Map<java.lang.String, java.lang.Object>]
Action:
Update your application's configuration
key为英文的情况:
yaml文件:
person:
score:
yuwen: 90
shuxue: 95
结果:
{"score":{"yuwen":10,"shuxue":10}} // 配置绑定成功
最后
以上就是无语帅哥为你收集整理的yaml语法 关于key为中文时的问题的全部内容,希望文章能够帮你解决yaml语法 关于key为中文时的问题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复