如何在spring boot应用程序中从yaml文件中读取json作为字符串?
我需要将json作为字符串/ json对象。
Application.yml
Shirt:
rest:
booking:
invoiceAddress: {"name":"VIA Pics AB","address1":"VIA Pics AB","address2":"Mejerivägen 3","zipCode":"11743","city":"STOCKHOLM","countryCode":"SE","email":"w.ho@Pics.fr"}
配置类
@Configuration
public class ConfigurationServiceClientConfiguration {
@Value("${Shirt.rest.booking.invoiceAddress}")
private String invoiceAddress;
@Bean
public String getInvoiceAddress(){
System.out.println(invoiceAddress);
return this.invoiceAddress;
}
}
还试过@JsonProperty,但不知道我的用法是否错误!
注意: 我还没有准备好将json更改为列表,因为更多的jsons(invoiceAddress)来了,我将不得不为每个列表制作列表。如果可以将json复制到yaml文件中,对我来说很容易。
答案 0 :(得分:4)
如果您想要作为计划字符串,则应在单引号“您的值”之间添加此值。在您的yml文件中
Shirt:
rest:
booking:
invoiceAddress: '{"name":"VIA Pics AB","address1":"VIA Pics AB","address2":"Mejerivägen 3","zipCode":"11743","city":"STOCKHOLM","countryCode":"SE","email":"w.ho@Pics.fr"}'
答案 1 :(得分:0)
尝试修改yml文件,如下所示:
Shirt.rest.booking: invoiceAddress: "\"name\":\"VIA Pics AB\",\"address1\":\"VIA Pics AB\",\"address2\":\"Mejerivägen 3\",\"zipCode\":\"11743\",\"city\":\"STOCKHOLM\",\"countryCode\":\"SE\",\"email\":\"w.ho@Pics.fr\""
答案 2 :(得分:0)
单引号转义YAML属性值中的所有特殊字符。 只需尝试将字符串添加到单引号'PropertiesValue'。
例如:
invoiceAddress: '{"name":"VIA Pics AB","address1":"VIA Pics AB","address2":"Mejerivägen 3","zipCode":"11743","city":"STOCKHOLM","countryCode":"SE","email":"w.ho@Pics.fr"}'