有人可以告诉我SnakeYaml用法v1.19中的错误。不知怎的,它无法序列化对象,我不知道为什么。
我的JavaBeanObject.java
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ConfigRunner {
private static File myfile = new File(Paths.get(System.getProperty("user.home"),"config", "trial.yml").toString());
private static String mypath =myfile.getPath();
private JavaBeanObject myparam;
public static JavaBeanObject getConfiguration(String filePath) throws IOException{
Constructor constructor = new Constructor(JavaBeanObject.class);
Yaml yaml = new Yaml(constructor);
try (InputStream in = Files.newInputStream(Paths.get(filePath))) {
JavaBeanObject config = yaml.loadAs(in, JavaBeanObject.class);
System.out.println(config.toString());
return config;
}
}
public static void main(String[] args) {
try {
System.out.println(mypath);
getConfiguration(mypath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我的ConfigRunner:
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ConfigRunner {
private static File myfile = new File(Paths.get(System.getProperty("user.home"),"config", "trial.yml").toString());
private static String mypath =myfile.getPath();
private JavaBeanObject myparam;
public static JavaBeanObject getConfiguration(String filePath) throws IOException{
Constructor constructor = new Constructor(JavaBeanObject.class);
Yaml yaml = new Yaml(constructor);
try (InputStream in = Files.newInputStream(Paths.get(filePath))) {
JavaBeanObject config = yaml.loadAs(in, JavaBeanObject.class);
System.out.println(config.toString());
return config;
}
}
public static void main(String[] args) {
try {
System.out.println(mypath);
getConfiguration(mypath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
First: anton
Second: berta
Third: caesar
Fourth: Dora
好的,我在这里面临的问题是使用Stringbuilder,它无法序列化对象。我得到的错误是:
C:\用户\多米诺\配置\ trial.yml 线程" main"中的例外情况无法创建property = First for JavaBean = First:null 第一:null 第二:null 第三名:null 第四:null
读者',第1行第1列: 第一:' anton ... ^ at org.yaml.snakeyaml.constructor.Constructor $ ConstructMapping.constructJavaBean2ndStep(Constructor.java:313) at org.yaml.snakeyaml.constructor.Constructor $ ConstructMapping.construct(Constructor.java:190) at org.yaml.snakeyaml.constructor.Constructor $ ConstructYamlObject.construct(Constructor.java:346) at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182) 在org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:141) at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:127) 在org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450) 在org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:444) 在ConfigRunner.getConfiguration(ConfigRunner.java:26)
引起:org.yaml.snakeyaml.error.YAMLException:无法找到属性' First' on class:JavaBeanObject 在org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:132) 在org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:121) at org.yaml.snakeyaml.constructor.Constructor $ ConstructMapping.getProperty(Constructor.java:323) at org.yaml.snakeyaml.constructor.Constructor $ ConstructMapping.constructJavaBean2ndStep(Constructor.java:241) 在ConfigRunner.main(ConfigRunner.java:36)
有人可以帮助解释错误吗?