{"id":107,"date":"2023-02-18T09:50:51","date_gmt":"2023-02-18T01:50:51","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=107"},"modified":"2023-02-18T09:51:42","modified_gmt":"2023-02-18T01:51:42","slug":"java-reflect-setaccessible-true-security-check-failed","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/java-reflect-setaccessible-true-security-check-failed\/","title":{"rendered":"Java\u53cd\u5c04setAccessible(true)\u5b89\u5168\u68c0\u67e5\u4e0d\u901a\u8fc7"},"content":{"rendered":"<h2>Bean\u8f6cMap<\/h2>\n<pre><code class=\"language-java\">public static Map&lt;String, Object&gt; beanToMap(Object object) throws Exception {\n    Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();\n\n    Class cls = object.getClass();\n    Field[] fields = cls.getDeclaredFields();\n    for (Field field : fields) {\n        field.setAccessible(true);\n        map.put(field.getName(), field.get(object));\n    }\n    return map;\n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4f7f\u7528\u4e86<code>setAccessible(true)<\/code>\u516c\u53f8\u5b89\u5168\u68c0\u67e5\u4e0d\u901a\u8fc7<\/p>\n<pre><code class=\"language-java\">public static Map&lt;String, Object&gt; beanToMap(Object obj) {\n    if (obj == null) {\n        return null;\n    }\n    Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();\n    try {\n        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());\n        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();\n        for (PropertyDescriptor property : propertyDescriptors) {\n            String key = property.getName();\n            \/\/ \u8fc7\u6ee4class\u5c5e\u6027\n            if (!key.equals(&quot;class&quot;)) {\n                \/\/ \u5f97\u5230property\u5bf9\u5e94\u7684getter\u65b9\u6cd5\n                Method getter = property.getReadMethod();\n                Object value = getter.invoke(obj);\n                map.put(key, value);\n            }\n        }\n    } catch (Exception e) {\n        logger.error(&quot;transBean2Map Error &quot;, e);\n    }\n    return map;\n}<\/code><\/pre>\n<p>\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5b89\u5168\u53ef\u9760<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bean\u8f6cMap public static Map&lt;String, Object&gt; beanTo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}