如何使用BeanUtils内省获取Java对象的所有属性列表? [英] How to get the list of all attributes of a Java object using BeanUtils introspection?

查看:919
本文介绍了如何使用BeanUtils内省获取Java对象的所有属性列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有获取POJO作为参数的方法。现在我想以编程方式获取POJO的所有属性(因为我的代码可能不知道运行时它的所有属性是什么),并且还需要获取属性的值。最后,我将形成POJO的字符串表示。

I have method which gets a POJO as it's parameter. Now I want to programmatically get all the attributes of the POJO (because my code may not know what are all the attributes in it at run time) and need to get the values for the attributes also. Finally I'll form a string representation of the POJO.

我可以使用 ToStringBuilder ,但我希望以特定于我的要求的特定格式构建输出字符串。

I could use ToStringBuilder, but I want build my output string in certain format specific to my requirement.

是否有可能在Beanutils中这样做!如果是,那么指向方法名称的指针是什么?如果不是,我应该编写自己的反射代码吗?

Is it possible to do so in Beanutils !? If yes, any pointers to the method name? If no, should I write my own reflection code?

推荐答案

你试过 ReflectionToStringBuilder ?看起来应该做你所描述的。

Have you tried ReflectionToStringBuilder? It looks like is should do what you describe.

这篇关于如何使用BeanUtils内省获取Java对象的所有属性列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆