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

2022/1/4 22:33:40

本文主要是介绍记一次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());
                    }



这篇关于记一次yaml转Java对象没有无参构造的异常的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程