如何将 Java 对象(bean)转换为键值对(反之亦然)? [英] How to convert a Java object (bean) to key-value pairs (and vice versa)?

查看:68
本文介绍了如何将 Java 对象(bean)转换为键值对(反之亦然)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个非常简单的 java 对象,它只有一些 getXXX 和 setXXX 属性.此对象仅用于处理值,基本上是记录或类型安全(和高性能)映射.我经常需要将此对象转换为键值对(字符串或类型安全)或从键值对转换为此对象.

Say I have a very simple java object that only has some getXXX and setXXX properties. This object is used only to handle values, basically a record or a type-safe (and performant) map. I often need to covert this object to key value pairs (either strings or type safe) or convert from key value pairs to this object.

除了反射或手动编写代码来进行这种转换,实现这一目标的最佳方法是什么?

Other than reflection or manually writing code to do this conversion, what is the best way to achieve this?

一个例子可能是通过 jms 发送这个对象,而不使用 ObjectMessage 类型(或将传入的消息转换为正确类型的对象).

An example might be sending this object over jms, without using the ObjectMessage type (or converting an incoming message to the right kind of object).

推荐答案

总是有 apache commons beanutils 但当然它使用了引擎盖下的反射

There is always apache commons beanutils but of course it uses reflection under the hood

这篇关于如何将 Java 对象(bean)转换为键值对(反之亦然)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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