Java仅循环打印第一个值-BuilderPattern

时间:2019-07-07 08:00:29

标签: object jackson builder-pattern

我正在尝试实验和学习BuilderPattern,同时读取json文件。我的目标是使用从json文件获得的数据创建对象。 json文件的外观如下:

[{
        "firstName": "Git",
        "lastName": "Hub",
        "website": "howtodoinjava.com"
    },
    {
        "firstName": "Brian",
        "lastName": "Schultz"
    }
]

第1类:员工类

public class Employee {

    private String firstName; // required
    private String lastName; // required
    private String website; // optional

    //Only has setters
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

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

    public void setWebsite(String website) {
        this.website = website;
    }

    //no public constructor. So the only way to get a Employee object is through the EmployeeBuilder class.
    private Employee(EmployeeBuilder builder) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.website = website;
    }

    public static class EmployeeBuilder {

        private String firstName; // required
        private String lastName; // required
        private String website; // optional

        public EmployeeBuilder() throws IOException {
        }

        public EmployeeBuilder requiredFirstName(String firstName) {
            this.firstName = firstName;
            return this;
        }

        public EmployeeBuilder requiredLastName(String lastName) {
            this.lastName = lastName;
            return this;
        }

        public EmployeeBuilder optionalWebsite(String website) {
            this.website = website;
            return this;
        }

        //Return the finally constructed User object
        public Employee build() {
            Employee emp = new Employee(this);
            return emp;
        }

    }
}

Class2:用户类别

public class User {

    String firstName;
    String lastName;
    String website;

    //Using Getters to get values from Json
    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getWebsite() {
        return website;
    }
}

3 主班

public class Main {

    public static void main(String[] args) throws IOException {

        ObjectMapper object = new ObjectMapper();

        User[] user = object.readValue(new File("C:\\MyTemp\\jackson.json"), User[].class);
        //Employee employee= new Employee.EmployeeBuilder().requiredFirstName(person.getFirstName()).requiredLastName(person.getLastName()).optionalWebsite(person.getWebsite()).build();

        List<User> emp1 = object.readValue("C:\\MyTemp\\jackson.json", new TypeReference<List<User>>() {});
        emp1.stream().forEach(x -> System.out.println(x));

        /*for (User person : user) {
            Employee employee= new Employee.EmployeeBuilder().requiredFirstName(person.getFirstName()).build();
            System.out.println(employee);
        } */
    }
}

问题:运行此代码时,我得到的只是第一名。吉特和布莱恩。我没有姓氏或网站。

有人可以建议我缺少什么吗?预先感谢您的宝贵时间。

0 个答案:

没有答案