如何从输出字符串中删除 POJO 类名?

问题描述 投票:0回答:3

我有以下POJO

package com.beans.test;

import lombok.Data;

@Data
public class Person {

    private int id;

    private String name;

    private Integer age;

    private Boolean isEmployed;

    private String address;

}

查询获取人员信息

public class testTable {

public Person getPersonInfo(final int id) {

return dsl.select(PERSON_INFO.NAME,
                  PERSON_INFO.AGE,
                  PERSON_INFO.IS_EMPLOYED,
                  PERSON_INFO.ADDRESS)
          .from(PERSON_INFO)
          .where(PERSON_INFO.ID.equals(id))
          .fetchOneInto(Person.class)
}
}

我需要将查询的输出打印为字符串。

Person person = testTable.getPersonInfo(id);
String personInfo = person.toString();
System.out.println("personInfo = ", personInfo);

输出:-

personInfo = Person(name=TestName,age=TestAge,isEmployed=true,address=TestAddress)

期望的输出:-

personInfo = name=TestName,age=TestAge,isEmployed=true,address=TestAddress

如何从所需的输出字符串中删除 POJO 类名?(如所需输出所示)

java output tostring jooq pojo
3个回答
1
投票

我的解决方案

package com.company;

import lombok.Data;

public class Main {

    @Data
    public static class Person {

        private int id;

        private String name;

        private Integer age;

        private Boolean isEmployed;

        private String address;

    }

    public static String getClassName(Class clazz){
        if(clazz.getDeclaringClass() == null)
        {
            return clazz.getSimpleName();
        }
        return getClassName(clazz.getDeclaringClass()) + "." + clazz.getSimpleName(); //Recursive - complete class name.
    }

    public static String toStringRemovingClassName(Object obj)
    {
        String pattern = String.format("^%s\\(|\\)$", getClassName(obj.getClass())); //Matches "<ClassName>(" at begin or ")" at end.
        return obj.toString().replaceAll(pattern, "");
    }

    public static void main(String[] args) {
        var person = new Person();
        System.out.println("personInfo = " + toStringRemovingClassName(person));
        person.id = 1;
        person.address = "\"952 NW 80th St, Miami, FL 33150\"";
        person.name = "Jeff";
        person.age = 30;
        person.isEmployed = true;

        System.out.println("personInfo = " + toStringRemovingClassName(person));
    }
}

输出

personInfo = id=0, name=null, age=null, isEmployed=null, address=null
personInfo = id=1, name=Jeff, age=30, isEmployed=true, address="952 NW 80th St, Miami, FL 33150"


1
投票

您需要在

override
类中使用
toString()
POJO
方法,如下所示:

    @Data
    public class Person {
        public int id;
        public String name;
        public Integer age;
        public Boolean isEmployed;
        public String address;

        @NonNull
        @Override
        public String toString() {
            return "id=" + id + ",name=" + name +",age=" + age + ",isEmployed=" + isEmployed + ",address=" + address;
        }
    }

0
投票

可以使用 Apache commons-lang3 lib 和

org.apache.commons.lang3.builder.ToStringBuilder
功能,您可以在其中指定样式,例如

@Override
public String toString() {
  return new ToStringBuilder(this, ToStringStyle.NO_CLASS_NAME_STYLE)
           .append("id", id)
           .append("name", name)
           .append("age", age)
           .toString()
}
© www.soinside.com 2019 - 2024. All rights reserved.