【问题标题】:Exception getting when Yaml Property binding in Java在 Java 中绑定 Yaml 属性时出现异常
【发布时间】:2019-06-27 09:51:43
【问题描述】:

我想要激活的是来自YAML 文件的类似内容

System.out.println("US City====>"+addressProperties.getFromaddress().get("US").getCity());
System.out.println("US City====>"+addressProperties.getFromaddress().get("US").getCountry());
System.out.println("US City====>"+addressProperties.getFromaddress().get("US").getPostalCode());

System.out.println("DE City====>"+addressProperties.getFromaddress().get("DE").getCity());
System.out.println("DE City====>"+addressProperties.getFromaddress().get("DE").getCountry());
System.out.println("DE City====>"+addressProperties.getFromaddress().get("DE").getPostalCode());

我有一个如下所示的yaml文件内容

address:
    fromaddress:
        US:
            country: US
            city: New York
            postalcode: 44444
            region: NY
        DE:
            country: DE
            city: Munich
            postalcode: 33333
            region: BE
        :
        :
        :
        :

我已经为上述山药内容创建了 DTO,如下所示

@Data
public abstract class AddressProperties extends HashMap<String, FromAddress> {
}

@Data
@Builder
public class FromAddress
{
    private String country;

    private String city;

    private String postal;

    private String region;
}

@Data
@ConfigurationProperties(prefix = ”address”)
@Service
public class FromAddressProperties
{
    public AddressProperties fromaddress;
}

但我得到以下异常

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘addressService' defined in file [/Users/xxxx/xxxx/workspaces/xxxx/target/classes/com/xxx/liita/xxxx/services/AddressService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘fromAddressProperties': Could not bind properties to FromAddressProperties (prefix=address, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'fromaddress[US]' of bean class [com.xxx.xxx.xxx.xxx.AddressProperties]: Illegal attempt to get property 'fromaddress' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'fromaddress' of bean class [com.xxx.xxx.xxx.xxx.FromAddressProperties]: Could not instantiate property type [com.xxx.xxx.xxx.xxx.FromAddressProperties] to auto-grow nested property path; nested exception is java.lang.IllegalArgumentException: Could not instantiate Map type: com.xxx.xxx.xxx.domains.FromAddressProperties

bean 类 [com.xxx.xxx.xxx.xxx.AddressProperties] 的无效属性 'fromaddress[US]':非法尝试获取属性 'fromaddress' 引发异常

谁能帮我解决这个问题,想知道我哪里出错了

更新 1

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xxxxx</groupId>
    <artifactId>xxxx-xxxxxx</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>xxx-xxxxx</name>
    <description>Sample Test</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.12</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

【问题讨论】:

  • fromaddressFromAddressProperties 的字段 AddressPropertie 的类型是什么?
  • 抱歉打错字了
  • 你能提供你的 pom.xml 吗(如果它不是那么大)
  • @HereAndBeyond 查看我的更新

标签: java data-binding yaml


【解决方案1】:

您的类和 yaml 文件中有一些错误。

  1. yaml 文件:
address:
 fromaddress:
   US:
     country: US
     city: New York
     postalcode: 44444
     region: NY
   DE:
     country: DE
     city: Berlin
     postalcode: 33333
     region: BE
  1. Spring 没有将您的类FromAddress 视为一个bean。给它添加像@Component 这样的注解。您应该将字段 postal 重命名为 postalcode(在 yaml 中使用邮政编码)
@Data
@Component
public class FromAddress
{
    private String country;

    private String city;

    private String postalcode;

    private String region;
}

  1. 你不需要上课AddressProperties

  2. 正确FromAddressProperties

@Data
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "address")
@Component
public class FromAddressProperties {
    public Map<String, FromAddress> fromaddress = new HashMap<>();
}
  1. 我要测试的主要课程
@SpringBootApplication
public class BindingProblemApplication implements CommandLineRunner {
    @Autowired
    FromAddressProperties addressProperties;

    public static void main(String[] args) {
        SpringApplication.run(BindingProblemApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("US City====>"+addressProperties.getFromaddress().get("US").getCity());
        System.out.println("US City====>"+addressProperties.getFromaddress().get("US").getCountry());
        System.out.println("US City====>"+addressProperties.getFromaddress().get("US").getCity());
        System.out.println("US City====>"+addressProperties.getFromaddress().get("US").getCountry());
        System.out.println("US City====>"+addressProperties.getFromaddress().get("US").getPostalcode());

        System.out.println("DE City====>"+addressProperties.getFromaddress().get("DE").getCity());
        System.out.println("DE City====>"+addressProperties.getFromaddress().get("DE").getCountry());
        System.out.println("DE City====>"+addressProperties.getFromaddress().get("DE").getPostalcode());
    }
}

【讨论】:

    【解决方案2】:

    只需将@NoArgsConstructor@AllArgsConstructor 添加到您的FromAddress,因为spring 需要默认构造函数和所有arg 构造函数来实例化FromAddress

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public class FromAddress
    {
        private String country;
    
        private String city;
    
        private String postal;
    
        private String region;
    }
    

    同时从 AddressProperties 类中删除 abstract,如下所示

    @Data
    public class AddressProperties extends HashMap<String, FromAddress> {
    }
    

    休息一切看起来都很好

    【讨论】:

      猜你喜欢
      • 2015-04-11
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      相关资源
      最近更新 更多