Eclipse 提供了为每个类自动生成 toString 方法的选项。
进一步利用此功能,我正在创建字符串格式模板,以便在 eclipse 生成 toString 方法时以 Json 格式提供。
我使用了以下字符串格式模板:
{ ${member.name()}:"${member.value}", ${otherMembers}}
Run Code Online (Sandbox Code Playgroud)
现在我按照 POJO 生成了 toString 方法,但是当我运行这个程序时,我得到的结果不是 VALID JSON。
{ name:"null", reportees:"[1, 2, 3]", department:"[retail, banking, finance]", owns:"null", supplimentary:"null}
Run Code Online (Sandbox Code Playgroud)
代码
public class TestPojo {
private String name;
private List<String> reportees;
private String[] department;
private Machine owns;
private List<Machine> supplimentary;
public static void main(String arg[]) {
TestPojo aTestPojo = new TestPojo();
aTestPojo.department = new String[] { "retail", "banking", "finance" };
aTestPojo.reportees = new ArrayList<String>() {
{
add("1");
add("2"); …Run Code Online (Sandbox Code Playgroud) 是否有任何标准的bean验证批注来检查Java集合中的重复项。或任何人都实现了自定义验证,以使用bean验证来检查列表中的重复项。
例如
public class MySecurityRequest{
private Date dob;
@DuplicateNotAllowed
private List securityQuestions;
}
public class SecurityQuestion{
private String question;
private String answer;
}
Run Code Online (Sandbox Code Playgroud)
在这里,我需要确保列表中的问题不再重复。是否有人面对过类似的问题?