记一次yaml转Java对象没有无参构造的异常

private  void  method(){
    InputStream in;
    try {
        Yaml yaml = new Yaml();
        in = this.getClass().getResourceAsStream("/ratelimiter-rule.yaml");
        if (in != null) {
            RuleConfig ruleConfig = yaml.loadAs(in, RuleConfig.class);
            System.out.println(ruleConfig.toString());
            Object obj =yaml.load(in);
            System.out.println(obj);
        }
    } catch (Exception e) { e.printStackTrace(); }
}

如上为读取yaml文件转java对象代码;

抛出异常:Can't construct a java object for tag:yaml.org,2002:com.jk.RuleConfig; exception=java.lang.InstantiationException: NoSuchMethodException:com.jk.RuleConfig.<init>();

java.lang.InstantiationException: NoSuchMethodException:com.jk.RuleConfig.<init>()

研究发现:RuleConfig类中的有参构造覆盖了无参构造导致的,添加无参构造方法即可。

回忆发现,其实在很多将数据转换为java对象时都会需要无参构造方法,比如通过数据库的数据转换成Entity对象,没有无参构造也会有相似的问题。

还有一个问题:配置文件如下,发现,yaml文件如果是 configs ,解析就可以,其他字段就不行,

configs:
  - appId: app-1
    limits:
      - api: /v1/user
        limit: 100
        unit: 60
      - api: /v1/order
        limit: 50

最开始以为和RuleConfig类中的成员变量是一致的,验证发现,就是成员变量换个其他名字,也是可以的。故不知问题所在。附上错误异常,是将配置文件中configs改为configs2的异常。

Can't construct a java object for tag:yaml.org,2002:com.jk.RuleConfig; exception=null
 in 'reader', line 1, column 1:
    configs2:

查看源码发现,其实正常情况下configs 和成员变量名称保持一致是可以的,因为成员变量的set方法命名就是根据set+成员变量名称来定义的。而我这里不行是因为我只修改了成员变量没有修改set方法,可以说set方法不是常规,在通过yaml解析为RuleConfig对象时是通过反射获取类中的方法,解析方法名来判断和yaml中定义的变量(也就是configs )是否一致,如果一致还会判断此方法是不是writable的,表示这个方法是不是写方法,如果是则校验合法可以构建对象,如果不是则校验失败跑出异常。

// public class PropertyUtils 类,获取类的成员方法,解析后properties 中。

protected Map<String, Property> getPropertiesMap(Class<?> type, BeanAccess bAccess) {
        if (propertiesCache.containsKey(type)) {
            return propertiesCache.get(type);
        }

        Map<String, Property> properties = new LinkedHashMap<String, Property>();
        boolean inaccessableFieldsExist = false;
        switch (bAccess) {
            case FIELD:
             // ...
                break;
            default:
                // add JavaBean properties
                try {
                    for (PropertyDescriptor property : Introspector.getBeanInfo(type)
                            .getPropertyDescriptors()) {
                        Method readMethod = property.getReadMethod();
                        if ((readMethod == null || !readMethod.getName().equals("getClass"))
                                && !isTransient(property)) {
                            properties.put(property.getName(), new MethodProperty(property));
                        }
                    }
                } catch (IntrospectionException e) {
                    throw new YAMLException(e);
                }

                // add public fields
                // .....
        }
        if (properties.isEmpty() && inaccessableFieldsExist) {
            throw new YAMLException("No JavaBean properties found in " + type.getName());
        }
        propertiesCache.put(type, properties);
        return properties;
    }

 

public class Constructor extends SafeConstructor{ }中的
protected Object constructJavaBean2ndStep(MappingNode node, Object object)的方法片段。
try {
                    TypeDescription memberDescription = typeDefinitions.get(beanType);
                    Property property = memberDescription == null ? getProperty(beanType, key)
                            : memberDescription.getProperty(key);

                   // 此处就是判读之前放入properties 中的方法是不是writable

                    if (!property.isWritable()) {
                        throw new YAMLException("No writable property '" + key + "' on class: "
                                + beanType.getName());
                    }

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值