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

查看:512
本文介绍了如何将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天全站免登陆