
这里主要想讲一下关于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 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: Reason: No converter found capable of converting from type [java.lang.Integer] to type [java.util.Map ] Action: Update your application's configuration
key为英文的情况:
yaml文件:
person:
score:
yuwen: 90
shuxue: 95
结果:
{"score":{"yuwen":10,"shuxue":10}} // 配置绑定成功
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)