gpt4 book ai didi

java - 从 Yaml 文件加载 Java Builder 对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:44:27 25 4
gpt4 key购买 nike

我使用构建器模式创建了一个 Bean 类,但在从 yaml 文件创建对象时遇到了问题。

这是一个示例类(实际类很大,如果您想用示例回答,这只是一个摘录):

public class ClientBuilder {

private final String firstName;
private final String lastName;
private final String displayName;

private ClientBuilder(Builder builder) {
firstName = builder.firstName;
lastName = builder.lastName;
displayName = builder.displayName;
}

public static class Builder {

private final String displayName; // Mandatory Attribute

public Builder( String displayName ) {
this.displayName = displayName;
}

private String firstName;
private String lastName;

public Builder setFirstName(String firstName) {
this.firstName = firstName;
return this;
}

public Builder setLastName(String lastName) {
this.lastName = lastName;
return this;
}

public ClientBuilder build() {
return new ClientBuilder(this);
}
}

@Override
public String toString() {
StringBuffer sbf = new StringBuffer();
sbf.append("New Company Object: \n");
sbf.append("firstName : " + this.firstName + "\n");
sbf.append("lastName : " + this.lastName + "\n");
sbf.append("displayName : " + this.displayName + "\n");
return sbf.toString();
}
}

我正在使用 snakeyaml 加载文件,但任何 yaml api 都可以。由于 displayName 是强制参数,我想在创建实例时传递该值。可以在创建对象时传递其他参数,但我希望可以选择通过 yaml 文件加载它们。

如果我使用 java bean,我可以加载 yaml 文件。有没有办法实例化构建器对象?

我试过:

InputStream input = new FileInputStream(new File("src/main/resources/client.yaml"));
Yaml yaml = new Yaml();
Builder builder = new Builder("Display Name");
builder = (Builder) yaml.loadAs(input, ClientBuilder.Builder.class);
ClientBuilder client = builder.build();
System.out.println(client.toString());

但我得到以下错误:

Exception in thread "main" Can't construct a java object for tag:yaml.org,2002:com.xxx.xxx.xxx.ClientBuilder$Builder; exception=java.lang.NoSuchMethodException: com.xxx.xxx.xxx.ClientBuilder$Builder.<init>()
in 'reader', line 2, column 1:
firstName: "Jaypal"

最佳答案

SnakeYaml 是一个非常强大的库,它支持基于构造函数注入(inject)创建实例。

/**
* create JavaBean
*/
public void testGetBeanAssumeClass() {
String data = "--- !!org.yaml.snakeyaml.constructor.Person\nfirstName: Andrey\nage: 99";
Object obj = construct(data);
assertNotNull(obj);
assertTrue("Unexpected: " + obj.getClass().toString(), obj instanceof Person);
Person person = (Person) obj;
assertEquals("Andrey", person.getFirstName());
assertNull(person.getLastName());
assertEquals(99, person.getAge().intValue());
}

/**
* create instance using constructor arguments
*/
public void testGetConstructorBean() {
String data = "--- !!org.yaml.snakeyaml.constructor.Person [ Andrey, Somov, 99 ]";
Object obj = construct(data);
assertNotNull(obj);
assertTrue(obj.getClass().toString(), obj instanceof Person);
Person person = (Person) obj;
assertEquals("Andrey", person.getFirstName());
assertEquals("Somov", person.getLastName());
assertEquals(99, person.getAge().intValue());
}

Junit code sample可以在这里查看。所以你的代码仍然有效。您可能需要以适当的格式更改 yaml 内容。完成后,一切就绪。

关于java - 从 Yaml 文件加载 Java Builder 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29191756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com