{"id":10006043,"date":"2022-07-28T17:53:01","date_gmt":"2022-07-28T09:53:01","guid":{"rendered":"https:\/\/ityet.com\/?p=10006043"},"modified":"2022-07-28T17:53:01","modified_gmt":"2022-07-28T09:53:01","slug":"java-bean-%e8%bd%ac-map-%e7%9a%84%e5%b7%a8%e5%9d%91%ef%bc%8c%e6%b3%a8%e6%84%8f%e4%ba%86%ef%bc%81%ef%bc%81%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006043","title":{"rendered":"Java Bean \u8f6c Map \u7684\u5de8\u5751\uff0c\u6ce8\u610f\u4e86\uff01\uff01\uff01"},"content":{"rendered":"<p>\u4f5c\u8005\uff1a\u660e\u660e\u5982\u6708\u5b66\u957f\/<br \/>\u94fe\u63a5\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=eNSccDWuE3qCIRhTFfO1jg%3D%3D.lOiP8MBkBNrPsSOKRDnRBtMuzB5eIedwkZZaa1yYyWxf6rzYpaLG9i47mStHR0qf\" rel=\"nofollow\">https:\/\/juejin.cn\/post\/711807&#8230;<\/a><\/p>\n<h2>\u4e00\u3001\u80cc\u666f<\/h2>\n<p>\u6709\u4e9b\u4e1a\u52a1\u573a\u666f\u4e0b\u9700\u8981\u5c06 Java Bean \u8f6c\u6210 Map \u518d\u4f7f\u7528\u3002<\/p>\n<p>\u672c\u4ee5\u4e3a\u5f88\u7b80\u5355\u573a\u666f\uff0c\u4f46\u662f\u5751\u5f88\u591a\u3002<\/p>\n<h2>\u4e8c\u3001\u90a3\u4e9b\u5751<\/h2>\n<h3>2.0 \u6d4b\u8bd5\u5bf9\u8c61<\/h3>\n<pre><code class=\"java\">import lombok.Data;\n\nimport java.util.Date;\n\n@Data\npublic class MockObject extends  MockParent{\n\n    private Integer aInteger;\n\n    private Long aLong;\n\n    private Double aDouble;\n\n    private Date aDate;\n}\n<\/code><\/pre>\n<p>\u7236\u7c7b<\/p>\n<pre><code class=\"java\">import lombok.Data;\n\n@Data\npublic class MockParent {\n    private Long parent;\n}<\/code><\/pre>\n<h3>2.1 JSON \u53cd\u5e8f\u5217\u5316\u4e86\u7c7b\u578b\u4e22\u5931<\/h3>\n<h4>2.1.1 \u95ee\u9898\u590d\u73b0<\/h4>\n<p>\u5c06 Java Bean \u8f6c Map \u6700\u5e38\u89c1\u7684\u624b\u6bb5\u5c31\u662f\u4f7f\u7528  JSON \u6846\u67b6\uff0c\u5982 fastjson \u3001 gson\u3001jackson \u7b49\u3002 \u4f46\u4f7f\u7528 JSON \u5c06 Java Bean \u8f6c Map \u4f1a\u5bfc\u81f4\u90e8\u5206\u6570\u636e\u7c7b\u578b\u4e22\u5931\u3002 \u5982\u4f7f\u7528 fastjson \uff0c\u5f53\u5c5e\u6027\u4e3a Long \u7c7b\u578b\u4f46\u6570\u5b57\u5c0f\u4e8e Integer \u6700\u5927\u503c\u65f6\uff0c\u53cd\u5e8f\u5217\u6210 Map \u4e4b\u540e\uff0c\u5c06\u53d8\u4e3a  Integer \u7c7b\u578b\u3002<\/p>\n<p>maven \u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"xml\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/com.alibaba\/fastjson --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;com.alibaba&lt;\/groupId&gt;\n    &lt;artifactId&gt;fastjson&lt;\/artifactId&gt;\n    &lt;version&gt;2.0.8&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"java\">import com.alibaba.fastjson.JSON;\nimport com.alibaba.fastjson.TypeReference;\n\nimport java.util.Date;\nimport java.util.Map;\n\npublic class JsonDemo {\n\n    public static void main(String[] args) {\n        MockObject mockObject = new MockObject();\n        mockObject.setAInteger(1);\n        mockObject.setALong(2L);\n        mockObject.setADate(new Date());\n        mockObject.setADouble(3.4D);\n        mockObject.setParent(3L);\n\n       String json = JSON.toJSONString(mockObject);\n\n        Map&lt;String,Object&gt; map =  JSON.parseObject(json, new TypeReference&lt;Map&lt;String,Object&gt;&gt;(){});\n\n        System.out.println(map);\n    }\n}<\/code><\/pre>\n<p>\u7ed3\u679c\u6253\u5370\uff1a<\/p>\n<blockquote><p>{&#8220;parent&#8221;:3,&#8221;ADouble&#8221;:3.4,&#8221;ALong&#8221;:2,&#8221;AInteger&#8221;:1,&#8221;ADate&#8221;:1657299916477}<\/p><\/blockquote>\n<p>\u8c03\u8bd5\u622a\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042236193\" alt=\"\" title=\"\" loading=\"lazy\"> \u901a\u8fc7 Java  Visualizer \u63d2\u4ef6\u8fdb\u884c\u53ef\u89c6\u5316\u67e5\u770b\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042236194\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h4>2.2.2 \u95ee\u9898\u63cf\u8ff0<\/h4>\n<p>\u5b58\u5728\u4e24\u4e2a\u95ee\u9898 \uff081\uff09 \u901a\u8fc7 fastjson \u5c06 Java Bean \u8f6c\u4e3a Map \uff0c\u7c7b\u578b\u4f1a\u53d1\u751f\u8f6c\u53d8\u3002 \u5982 Long \u53d8\u6210 Integer \uff0cDate \u53d8\u6210 Long, Double \u53d8\u6210  Decimal \u7c7b\u578b\u7b49\u3002 \uff082\uff09\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\uff0cMap \u7684  key \u5e76\u975e\u548c\u5c5e\u6027\u540d\u5b8c\u5168\u5bf9\u5e94\uff0c\u50cf\u662f\u901a\u8fc7 get set \u65b9\u6cd5\u201c\u63a8\u65ad\u201d\u51fa\u6765\u7684\u5c5e\u6027\u540d\u3002<\/p>\n<h3>2.2 BeanMap \u8f6c\u6362\u5c5e\u6027\u540d\u9519\u8bef<\/h3>\n<h4>2.2.1 commons-beanutils \u7684 BeanMap<\/h4>\n<p>maven \u7248\u672c\uff1a<\/p>\n<pre><code class=\"xml\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/commons-beanutils\/commons-beanutils --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;commons-beanutils&lt;\/groupId&gt;\n    &lt;artifactId&gt;commons-beanutils&lt;\/artifactId&gt;\n    &lt;version&gt;1.9.4&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"java\">import org.apache.commons.beanutils.BeanMap;\nimport third.fastjson.MockObject;\n\nimport java.util.Date;\n\npublic class BeanUtilsDemo {\n    public static void main(String[] args) {\n        MockObject mockObject = new MockObject();\n        mockObject.setAInteger(1);\n        mockObject.setALong(2L);\n        mockObject.setADate(new Date());\n        mockObject.setADouble(3.4D);\n        mockObject.setParent(3L);\n\n        BeanMap beanMap = new BeanMap(mockObject);\n        System.out.println(beanMap);\n    }\n}<\/code><\/pre>\n<p>\u8c03\u8bd5\u622a\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042236195\" alt=\"\" title=\"\" loading=\"lazy\"> \u5b58\u5728\u548c cglib \u4e00\u6837\u7684\u95ee\u9898\uff0c\u867d\u7136\u7c7b\u578b\u6ca1\u95ee\u9898\u4f46\u662f\u5c5e\u6027\u540d\u8fd8\u662f\u4e0d\u5bf9\u3002<\/p>\n<p>\u539f\u56e0\u5206\u6790\uff1a<\/p>\n<pre><code class=\"java\">\/**\n * Constructs a new &lt;code&gt;BeanMap&lt;\/code&gt; that operates on the\n * specified bean.  If the given bean is &lt;code&gt;null&lt;\/code&gt;, then\n * this map will be empty.\n *\n * @param bean  the bean for this map to operate on\n *\/\npublic BeanMap(final Object bean) {\n    this.bean = bean;\n    initialise();\n}<\/code><\/pre>\n<p>\u5173\u952e\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"java\">private void initialise() {\n    if(getBean() == null) {\n        return;\n    }\n\n    final Class&lt;? extends Object&gt;  beanClass = getBean().getClass();\n    try {\n        \/\/BeanInfo beanInfo = Introspector.getBeanInfo( bean, null );\n        final BeanInfo beanInfo = Introspector.getBeanInfo( beanClass );\n        final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();\n        if ( propertyDescriptors != null ) {\n            for (final PropertyDescriptor propertyDescriptor : propertyDescriptors) {\n                if ( propertyDescriptor != null ) {\n                    final String name = propertyDescriptor.getName();\n                    final Method readMethod = propertyDescriptor.getReadMethod();\n                    final Method writeMethod = propertyDescriptor.getWriteMethod();\n                    final Class&lt;? extends Object&gt; aType = propertyDescriptor.getPropertyType();\n\n                    if ( readMethod != null ) {\n                        readMethods.put( name, readMethod );\n                    }\n                    if ( writeMethod != null ) {\n                        writeMethods.put( name, writeMethod );\n                    }\n                    types.put( name, aType );\n                }\n            }\n        }\n    }\n    catch ( final IntrospectionException e ) {\n        logWarn(  e );\n    }\n}<\/code><\/pre>\n<p>\u8c03\u8bd5\u4e00\u4e0b\u5c31\u4f1a\u53d1\u73b0\uff0c\u95ee\u9898\u51fa\u5728 <code>BeanInfo<\/code> \u91cc\u9762 <code>PropertyDescriptor<\/code> \u7684 name \u4e0d\u6b63\u786e\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042236196\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<p>\u7ecf\u8fc7\u5206\u6790\u4f1a\u53d1\u73b0 <code>java.beans.Introspector#getTargetPropertyInfo<\/code> \u65b9\u6cd5\u662f\u5b57\u6bb5\u89e3\u6790\u7684\u5173\u952e<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042236197\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"> \u5bf9\u4e8e\u65e0\u53c2\u7684\u4ee5 get \u5f00\u5934\u7684\u65b9\u6cd5\u540d\u4ece index =3 \u5904\u622a\u53d6\uff0c\u5982 getALong \u622a\u53d6\u540e\u4e3a ALong\uff0c \u5982 getADouble \u622a\u53d6\u540e\u4e3a ADouble\u3002<\/p>\n<p>\u7136\u540e\u53bb\u6784\u9020 <code>PropertyDescriptor<\/code>:<\/p>\n<pre><code class=\"java\">\/**\n * Creates &lt;code&gt;PropertyDescriptor&lt;\/code&gt; for the specified bean\n * with the specified name and methods to read\/write the property value.\n *\n * @param bean   the type of the target bean\n * @param base   the base name of the property (the rest of the method name)\n * @param read   the method used for reading the property value\n * @param write  the method used for writing the property value\n * @exception IntrospectionException if an exception occurs during introspection\n *\n * @since 1.7\n *\/\nPropertyDescriptor(Class&lt;?&gt; bean, String base, Method read, Method write) throws IntrospectionException {\n    if (bean == null) {\n        throw new IntrospectionException(&quot;Target Bean class is null&quot;);\n    }\n    setClass0(bean);\n    setName(Introspector.decapitalize(base));\n    setReadMethod(read);\n    setWriteMethod(write);\n    this.baseName = base;\n}<\/code><\/pre>\n<p>\u5e95\u5c42\u4f7f\u7528 <code>java.beans.Introspector#decapitalize<\/code> \u8fdb\u884c\u89e3\u6790\uff1a<\/p>\n<pre><code class=\"java\">\/**\n * Utility method to take a string and convert it to normal Java variable\n * name capitalization.  This normally means converting the first\n * character from upper case to lower case, but in the (unusual) special\n * case when there is more than one character and both the first and\n * second characters are upper case, we leave it alone.\n * &lt;p&gt;\n * Thus &quot;FooBah&quot; becomes &quot;fooBah&quot; and &quot;X&quot; becomes &quot;x&quot;, but &quot;URL&quot; stays\n * as &quot;URL&quot;.\n *\n * @param  name The string to be decapitalized.\n * @return  The decapitalized version of the string.\n *\/\npublic static String decapitalize(String name) {\n    if (name == null || name.length() == 0) {\n        return name;\n    }\n    if (name.length() &gt; 1 &amp;&amp; Character.isUpperCase(name.charAt(1)) &amp;&amp;\n                    Character.isUpperCase(name.charAt(0))){\n        return name;\n    }\n    char chars[] = name.toCharArray();\n    chars[0] = Character.toLowerCase(chars[0]);\n    return new String(chars);\n}<\/code><\/pre>\n<p>\u4ece\u4ee3\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u51fa (1) \u5f53 name \u7684\u957f\u5ea6 &gt; 1\uff0c\u4e14\u7b2c\u4e00\u4e2a\u5b57\u7b26\u548c\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u90fd\u5927\u5199\u65f6\uff0c\u76f4\u63a5\u8fd4\u56de\u53c2\u6570\u4f5c\u4e3a<code>PropertyDescriptor<\/code> name\u3002 (2) \u5426\u5219\u5c06 name \u8f6c\u4e3a\u9996\u5b57\u6bcd\u5c0f\u5199<\/p>\n<p>\u8fd9\u79cd\u5904\u7406\u672c\u610f\u662f\u4e3a\u4e86\u4e0d\u8ba9\u5c5e\u6027\u4e3a\u7c7b\u4f3c URL \u8fd9\u79cd\u7f29\u7565\u8bcd\u8f6c\u4e3a  uRL \uff0c\u7ed3\u679c\u201c\u8bef\u4f24\u201d\u4e86\u6211\u4eec\u8fd9\u79cd\u573a\u666f\u3002<\/p>\n<h4>2.2.2 \u4f7f\u7528 cglib \u7684 BeanMap<\/h4>\n<p>cglib \u4f9d\u8d56<\/p>\n<pre><code class=\"xml\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/cglib\/cglib --&gt;\n&lt;dependency&gt;\n        &lt;groupId&gt;cglib&lt;\/groupId&gt;\n        &lt;artifactId&gt;cglib-nodep&lt;\/artifactId&gt;\n        &lt;version&gt;3.2.12&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"java\">import net.sf.cglib.beans.BeanMap;\nimport third.fastjson.MockObject;\n\nimport java.util.Date;\n\npublic class BeanMapDemo {\n\n    public static void main(String[] args) {\n\n        MockObject mockObject = new MockObject();\n        mockObject.setAInteger(1);\n        mockObject.setALong(2L);\n        mockObject.setADate(new Date());\n        mockObject.setADouble(3.4D);\n        mockObject.setParent(3L);\n\n        BeanMap beanMapp = BeanMap.create(mockObject);\n\n        System.out.println(beanMapp);\n    }\n}<\/code><\/pre>\n<p>\u7ed3\u679c\u5c55\u793a\uff1a <img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042236198\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"> \u6211\u4eec\u53d1\u73b0\u7c7b\u578b\u5bf9\u4e86\uff0c\u4f46\u662f\u5c5e\u6027\u540d\u4f9d\u7136\u4e0d\u5bf9\u3002<\/p>\n<p>\u5173\u952e\u4ee3\u7801\uff1a <code>net.sf.cglib.core.ReflectUtils#getBeanGetters<\/code> \u5e95\u5c42\u4e5f\u4f1a\u7528\u5230   <code>java.beans.Introspector#decapitalize<\/code>  \u6240\u4ee5\u5c5e\u6027\u540d\u5b58\u5728\u4e00\u6837\u7684\u95ee\u9898\u5c31\u4e0d\u8db3\u4e3a\u5947\u4e86\u3002<\/p>\n<h2>\u4e09\u3001\u89e3\u51b3\u529e\u6cd5<\/h2>\n<h3>3.1 \u89e3\u51b3\u65b9\u6848<\/h3>\n<p>\u89e3\u51b3\u65b9\u6848\u6709\u5f88\u591a\uff0c\u672c\u6587\u63d0\u4f9b\u4e00\u4e2a\u57fa\u4e8e  dubbo\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>maven \u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"xml\">&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.apache.dubbo\/dubbo --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.dubbo&lt;\/groupId&gt;\n    &lt;artifactId&gt;dubbo&lt;\/artifactId&gt;\n    &lt;version&gt;3.0.9&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"java\">import org.apache.dubbo.common.utils.PojoUtils;\nimport third.fastjson.MockObject;\n\nimport java.util.Date;\n\npublic class DubboPojoDemo {\n    public static void main(String[] args) {\n        MockObject mockObject = new MockObject();\n        mockObject.setAInteger(1);\n        mockObject.setALong(2L);\n        mockObject.setADate(new Date());\n        mockObject.setADouble(3.4D);\n        mockObject.setParent(3L);\n\n        Object generalize = PojoUtils.generalize(mockObject);\n\n        System.out.println(generalize);\n    }\n}\n<\/code><\/pre>\n<p>\u8c03\u8bd5\u6548\u679c\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042236199\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\">Java Visualizer \u6548\u679c\uff1a <img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042236200\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<h3>3.2 \u539f\u7406\u89e3\u6790<\/h3>\n<p>\u6838\u5fc3\u4ee3\u7801\uff1a <code>org.apache.dubbo.common.utils.PojoUtils#generalize(java.lang.Object)<\/code><\/p>\n<pre><code class=\"java\">public static Object generalize(Object pojo) {\n     eturn generalize(pojo, new IdentityHashMap());\n}<\/code><\/pre>\n<p>\u5173\u952e\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"java\">\/\/ pojo \u5f85\u8f6c\u6362\u7684\u5bf9\u8c61\n\/\/ history \u7f13\u5b58 Map\uff0c\u63d0\u9ad8\u6027\u80fd\nprivate static Object generalize(Object pojo, Map&lt;Object, Object&gt; history) {\n    if (pojo == null) {\n        return null;\n    }\n\n     \/\/ \u679a\u4e3e\u76f4\u63a5\u8fd4\u56de\u679a\u4e3e\u540d\n    if (pojo instanceof Enum&lt;?&gt;) {\n        return ((Enum&lt;?&gt;) pojo).name();\n    }\n\n    \/\/ \u679a\u4e3e\u6570\u7ec4\uff0c\u8fd4\u56de\u679a\u4e3e\u540d\u6570\u7ec4\n    if (pojo.getClass().isArray() &amp;&amp; Enum.class.isAssignableFrom(pojo.getClass().getComponentType())) {\n        int len = Array.getLength(pojo);\n        String[] values = new String[len];\n        for (int i = 0; i &lt; len; i++) {\n            values[i] = ((Enum&lt;?&gt;) Array.get(pojo, i)).name();\n        }\n        return values;\n    }\n\n    \/\/ \u57fa\u672c\u7c7b\u578b\u8fd4\u56de pojo \u81ea\u8eab\n    if (ReflectUtils.isPrimitives(pojo.getClass())) {\n        return pojo;\n    }\n\n    \/\/ Class \u8fd4\u56de name\n    if (pojo instanceof Class) {\n        return ((Class) pojo).getName();\n    }\n\n    Object o = history.get(pojo);\n    if (o != null) {\n        return o;\n    }\n    history.put(pojo, pojo);\n\n\/\/ \u6570\u7ec4\u7c7b\u578b\uff0c\u9012\u5f52\n    if (pojo.getClass().isArray()) {\n        int len = Array.getLength(pojo);\n        Object[] dest = new Object[len];\n        history.put(pojo, dest);\n        for (int i = 0; i &lt; len; i++) {\n            Object obj = Array.get(pojo, i);\n            dest[i] = generalize(obj, history);\n        }\n        return dest;\n    }\n\/\/ \u96c6\u5408\u7c7b\u578b\u9012\u5f52\n    if (pojo instanceof Collection&lt;?&gt;) {\n        Collection&lt;Object&gt; src = (Collection&lt;Object&gt;) pojo;\n        int len = src.size();\n        Collection&lt;Object&gt; dest = (pojo instanceof List&lt;?&gt;) ? new ArrayList&lt;Object&gt;(len) : new HashSet&lt;Object&gt;(len);\n        history.put(pojo, dest);\n        for (Object obj : src) {\n            dest.add(generalize(obj, history));\n        }\n        return dest;\n    }\n    \/\/ Map \u7c7b\u578b\uff0c\u76f4\u63a5 \u5bf9 key \u548c value \u5904\u7406\n    if (pojo instanceof Map&lt;?, ?&gt;) {\n        Map&lt;Object, Object&gt; src = (Map&lt;Object, Object&gt;) pojo;\n        Map&lt;Object, Object&gt; dest = createMap(src);\n        history.put(pojo, dest);\n        for (Map.Entry&lt;Object, Object&gt; obj : src.entrySet()) {\n            dest.put(generalize(obj.getKey(), history), generalize(obj.getValue(), history));\n        }\n        return dest;\n    }\n    Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();\n    history.put(pojo, map);\n\n    \/\/ \u5f00\u542f\u751f\u6210 class \u5219\u5199\u5165 pojo \u7684class\n    if (GENERIC_WITH_CLZ) {\n        map.put(&quot;class&quot;, pojo.getClass().getName());\n    }\n\n  \/\/ \u5904\u7406 get \u65b9\u6cd5\n    for (Method method : pojo.getClass().getMethods()) {\n        if (ReflectUtils.isBeanPropertyReadMethod(method)) {\n            ReflectUtils.makeAccessible(method);\n            try {\n                map.put(ReflectUtils.getPropertyNameFromBeanReadMethod(method), generalize(method.invoke(pojo), history));\n            } catch (Exception e) {\n                throw new RuntimeException(e.getMessage(), e);\n            }\n        }\n    }\n    \/\/ \u5904\u7406\u516c\u6709\u5c5e\u6027\n    for (Field field : pojo.getClass().getFields()) {\n        if (ReflectUtils.isPublicInstanceField(field)) {\n            try {\n                Object fieldValue = field.get(pojo);\n                \/\/ \u5bf9\u8c61\u5df2\u7ecf\u89e3\u6790\u8fc7\uff0c\u76f4\u63a5\u4ece\u7f13\u5b58\u91cc\u8bfb\u63d0\u9ad8\u6027\u80fd\n                if (history.containsKey(pojo)) {\n                    Object pojoGeneralizedValue = history.get(pojo);\n                    \/\/ \u5df2\u7ecf\u89e3\u6790\u8fc7\u8be5\u5c5e\u6027\u5219\u8df3\u8fc7\uff08\u5982\u516c\u6709\u5c5e\u6027\uff0c\u4e14\u6709 get \u65b9\u6cd5\u7684\u60c5\u51b5\uff09\n                    if (pojoGeneralizedValue instanceof Map\n                        &amp;&amp; ((Map) pojoGeneralizedValue).containsKey(field.getName())) {\n                        continue;\n                    }\n                }\n                if (fieldValue != null) {\n                    map.put(field.getName(), generalize(fieldValue, history));\n                }\n            } catch (Exception e) {\n                throw new RuntimeException(e.getMessage(), e);\n            }\n        }\n    }\n    return map;\n}<\/code><\/pre>\n<p>\u5173\u952e\u622a\u56fe <img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042236201\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"> <img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042236202\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"><\/p>\n<pre><code>org.apache.dubbo.common.utils.ReflectUtils#getPropertyNameFromBeanReadMethod\npublic static String getPropertyNameFromBeanReadMethod(Method method) {\n    if (isBeanPropertyReadMethod(method)) {\n        \/\/ get \u65b9\u6cd5\uff0c\u5219\u4ece index =3 \u7684\u5b57\u7b26\u5c0f\u5199 + \u540e\u9762\u7684\u5b57\u7b26\u4e32\n        if (method.getName().startsWith(&quot;get&quot;)) {\n            return method.getName().substring(3, 4).toLowerCase()\n                    + method.getName().substring(4);\n        }\n        \/\/ is \u5f00\u5934\u65b9\u6cd5\uff0c index =2 \u7684\u5b57\u7b26\u5c0f\u5199 + \u540e\u9762\u7684\u5b57\u7b26\u4e32\n        if (method.getName().startsWith(&quot;is&quot;)) {\n            return method.getName().substring(2, 3).toLowerCase()\n                    + method.getName().substring(3);\n        }\n    }\n    return null;\n}<\/code><\/pre>\n<p>\u56e0\u6b64\uff0c getALong \u65b9\u6cd5\u5bf9\u5e94\u7684\u5c5e\u6027\u540d\u88ab\u89e3\u6790\u4e3a  aLong\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042236203\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" title=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" loading=\"lazy\"> \u540c\u65f6\uff0c\u8fd9\u4e48\u5904\u7406\u4e5f\u4f1a\u5b58\u5728\u95ee\u9898\u3002\u5982\u5f53\u5c5e\u6027\u540d\u53eb URL \u65f6\uff0c\u8f6c\u4e3a Map \u540e key \u5c31\u4f1a\u88ab\u89e3\u6790\u6210  uRL\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042236204\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4ece\u8fd9\u91cc\u770b\u51fa\uff0c\u5f53\u5c5e\u6027\u540d\u6bd4\u8f83\u7279\u6b8a\u65f6\u4e5f\u5f88\u5bb9\u6613\u51fa\u95ee\u9898\uff0c\u4f46 dubbo \u8fd9\u4e2a\u5de5\u5177\u7c7b\u66f4\u7b26\u5408\u6211\u4eec\u7684\u9884\u671f\u3002 \u66f4\u591a\u7ec6\u8282\uff0c\u5927\u5bb6\u53ef\u4ee5\u6839\u636e DEMO  \u81ea\u884c\u8c03\u8bd5\u5b66\u4e60\u3002<\/p>\n<p>\u5982\u679c\u60f3\u4e25\u683c\u548c\u5c5e\u6027\u4fdd\u6301\u4e00\u81f4\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cd\u5c04\u83b7\u53d6\u5c5e\u6027\u540d\u548c\u5c5e\u6027\u503c\uff0c\u52a0\u7f13\u5b58\u673a\u5236\u63d0\u5347\u89e3\u6790\u7684\u6548\u7387\u3002<\/p>\n<h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<p>Java Bean \u8f6c Map \u7684\u5751\u5f88\u591a\uff0c\u6700\u5e38\u89c1\u7684\u5c31\u662f\u7c7b\u578b\u4e22\u5931\u548c\u5c5e\u6027\u540d\u89e3\u6790\u9519\u8bef\u7684\u95ee\u9898\u3002 \u5927\u5bb6\u5728\u4f7f\u7528  JSON \u6846\u67b6\u548c Java Bean \u8f6c Map \u7684\u6846\u67b6\u65f6\u8981\u7279\u522b\u5c0f\u5fc3\u3002 \u5e73\u65f6\u4f7f\u7528\u67d0\u4e9b\u6846\u67b6\u65f6\uff0c\u591a\u5199\u4e00\u4e9b DEMO \u8fdb\u884c\u9a8c\u8bc1\uff0c\u591a\u8bfb\u6e90\u7801\uff0c\u591a\u8c03\u8bd5\uff0c\u5c11\u8d9f\u5751\u3002<\/p>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a href=\"https:\/\/link.segmentfault.com\/?enc=NEuKvltnts%2B930zFVUlVgQ%3D%3D.P1YOs3lZuFEHQG8z0KLih2y9ePSRgv0I4HLdbAscUmQ%3D\" rel=\"nofollow\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a href=\"https:\/\/link.segmentfault.com\/?enc=tPZwFz9yiUiPV%2FR8MNdetw%3D%3D.LDXSg1qOYpmRKe8FWplRBYrrxaMzJwYD1b1zrw6EwwQlcqmPDiX6Ejw4Mhfm0gcabN7zGlxB6BrJzB8PFa6nOrUn8uE9VTqR%2BIzFwipQPe0%3D\" rel=\"nofollow\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a href=\"https:\/\/link.segmentfault.com\/?enc=xQf6MbpVi44JoQWpWSbU%2BA%3D%3D.ahrii7MwZu4W6jL6DI61LcjYAXMPdxO4XKRMpbu80Bix%2FYCBnt%2BhhBohg2%2FaDgUJ\" rel=\"nofollow\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a href=\"https:\/\/link.segmentfault.com\/?enc=mNpxYCD%2BWqjIpiK74mb5FA%3D%3D.kgLOvSa4oMadm0xC2b%2F1TmqAWKi4X8KjO9HiBYL5UiXT6FZrzgiJ74k3t0Pdp20EeUeGRufaLG8Unpnb5xvKGx57dKN4%2B7MV%2BHn425FQp5Q%3D\" rel=\"nofollow\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a href=\"https:\/\/link.segmentfault.com\/?enc=agxEfrxvgxlBY4fEEExawg%3D%3D.UAqhufyi%2Ba16Tg6voY4HdQpzwcz%2FKgq%2B4xFNbGTqzMDk1%2FuoadXpzFUpKnUWnPm6dPJu7hVOT%2FCL7YGC4M9maYtW%2ByFgIJvvVvNZwVntLlGbGE3yGKKsqb1q25kGSb83\" rel=\"nofollow\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042236191\">https:\/\/segmentfault.com\/a\/1190000042236191<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u660e\u660e\u5982\u6708\u5b66\u957f\/\u94fe\u63a5\uff1ahttps:\/\/juejin.cn\/post\/711807&#8230; \u4e00\u3001\u80cc\u666f  [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006043","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006043"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006043\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}