如何使用反射或任何其他实用程序完全打印 Java Bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11669240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print Java Bean completely using reflection or any other utility
提问by sridhar
I have an Java bean class Person
containing 3 variables:
我有一个Person
包含 3 个变量的 Java bean 类:
- name (String)
- age (String)
- address (Object).
- 名称(字符串)
- 年龄(字符串)
- 地址(对象)。
Address
contains 3 variables:
Address
包含 3 个变量:
- street
- door_no
- city
- 街道
- 门号
- 城市
I would like to have a utility which should print all variables in Person
.
我想要一个实用程序,它应该在Person
.
By this I mean it should print Person
& also the Address
object contained in it.
我的意思是它应该打印Person
以及其中Address
包含的对象。
I could create a hashmap and put the variable names & values using reflection and print key/value UI in JSP but the issue is I have to apply reflection for Address
to add variable/value in the hashmap.
我可以创建一个哈希图并使用反射将变量名和值放在 JSP 中并打印键/值 UI,但问题是我必须应用反射Address
来在哈希图中添加变量/值。
Is there a utility available to do this?
是否有可用的实用程序来执行此操作?
回答by Francisco Spaeth
You could use ToStringBuilderfrom Apache Commons.
你可以使用ToStringBuilder从Apache的百科全书。
From documentation:
从文档:
A typical invocation for this method would look like:
public String toString() { return ToStringBuilder.reflectionToString(this); }
此方法的典型调用如下所示:
public String toString() { return ToStringBuilder.reflectionToString(this); }
More details:
更多细节:
This class enables a good and consistent toString() to be built for any class or object. This class aims to simplify the process by:
allowing field names handling all types consistently handling nulls consistently outputting arrays and multi-dimensional arrays enabling the detail level to be controlled for Objects and Collections handling class hierarchies To use this class write code as follows:
public class Person { String name; int age; boolean smoker; ... public String toString() { return new ToStringBuilder(this). append("name", name). append("age", age). append("smoker", smoker). toString(); } }
Alternatively, there is a method that uses reflection to determine the fields to test. Because these fields are usually private, the method, reflectionToString, uses AccessibleObject.setAccessible to change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are set up correctly. It is also slower than testing explicitly. A typical invocation for this method would look like:
public String toString() { return ToStringBuilder.reflectionToString(this); }
此类可以为任何类或对象构建良好且一致的 toString()。本课程旨在通过以下方式简化流程:
允许处理所有类型的字段名称一致地处理空值一致地输出数组和多维数组允许控制对象和集合处理类层次结构的详细级别使用此类编写代码如下:
public class Person { String name; int age; boolean smoker; ... public String toString() { return new ToStringBuilder(this). append("name", name). append("age", age). append("smoker", smoker). toString(); } }
或者,有一种方法使用反射来确定要测试的字段。由于这些字段通常是私有的,reflectionToString 方法使用 AccessibleObject.setAccessible 来更改字段的可见性。这将在安全管理器下失败,除非正确设置了适当的权限。它也比显式测试慢。此方法的典型调用如下所示:
public String toString() { return ToStringBuilder.reflectionToString(this); }
回答by Romain
You're probably looking for Apache Commons ToStringBuilder#reflectionToString(Object)
.
您可能正在寻找 Apache Commons ToStringBuilder#reflectionToString(Object)
。
回答by Jeff Storey
You can implement the toString
method to print out whatever you want, or you can use Apache Commons ToStringBuilder
http://commons.apache.org/lang/api-2.3/org/apache/commons/lang/builder/ToStringBuilder.htmland its reflectionToString
method. I don't believe this will recurse through the properties (like address for example), but if you want to see Address printed out with Street, Door No and City, use implement its toString
method to print that information out.
您可以实现该toString
方法以打印出您想要的任何内容,或者您可以使用 Apache Commons ToStringBuilder
http://commons.apache.org/lang/api-2.3/org/apache/commons/lang/builder/ToStringBuilder.html及其reflectionToString
方法. 我不相信这会通过属性(例如地址)递归,但是如果您想看到地址与街道、门牌号和城市一起打印出来,请使用实现其toString
方法来打印该信息。
回答by Steve Oh
You may find the Hymanson JSON serializeruseful for this purpose. The Hymanson library may already be part of your stack. If not, find the required dependencies below.
您可能会发现 Hymanson JSON 序列化程序对此很有用。Hymanson 库可能已经是您堆栈的一部分。如果没有,请在下面找到所需的依赖项。
private static final ObjectMapper OBJECT_MAPPER_SINGLETON = new ObjectMapper();
public static String toStringUsingHymanson(final Object object) {
try {
return OBJECT_MAPPER_SINGLETON.writeValueAsString(object);
} catch (final JsonProcessingException e) {
return String.valueOf(object);
}
}
Sample output:
示例输出:
{"name":"John Doe","age":42}
{"name":"John Doe","age":42}
Required maven/gradle dependencies:
所需的maven/gradle 依赖项:
- Hymanson-core, groupId=com.fasterxml.Hymanson.core
- Hymanson-databind, groupId=com.fasterxml.Hymanson.core
- Hyman逊核心,groupId=com.fasterxml.Hymanson.core
- Hyman逊数据绑定,groupId=com.fasterxml.Hymanson.core