jackson 的UnrecognizedPropertyException錯誤

<div id="blog_content" class="hide-main-content"> <div class="hide-article-box text-center" style="display: none;"> <a class="btn" id="btn-readmore">閱讀更多</a> </div> <p>&nbsp;</p> <div style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; margin-top: 8px; margin-right: 8px; margin-bottom: 8px; margin-left: 8px; background-color: #ffffff;"> <p>前段時間,使用jackson封裝了json字符串轉換爲javabean的方法,代碼以下:</p> <div class="dp-highlighter" id=""><div class="bar"><div class="tools">Java代碼 <embed wmode="transparent" src="/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=public%20static%20%3CT%3E%20T%20renderJson2Object(String%20json%2C%20Class%20clazz)%7B%0A%20%20%20%20%20%20%20if(!StringUtil.checkObj(json))%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20null%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20try%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS%2C%20false)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20DateFormat%20myDateFormat%20%3D%20new%20SimpleDateFormat(%22yyyy-MM-dd%20HH%3Amm%3Ass%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20mapper.getSerializationConfig().setDateFormat(myDateFormat)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20(T)mapper.readValue(json%2C%20clazz)%3B%0A%09%09%7D%20catch%20(IOException%20e)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20log.error(e)%3B%0A%09%09%09throw%20new%20IllegalArgumentException(e)%3B%0A%09%09%7D%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="javascript:void()" title="收藏這段代碼" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="/images/icon_star.png" alt="收藏代碼"><img class="spinner" src="/images/spinner.gif" style="display:none"></a></div></div><ol start="1" class="dp-j"><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;&lt;T&gt;&nbsp;T&nbsp;renderJson2Object(String&nbsp;json,&nbsp;Class&nbsp;clazz){&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">if</span><span>(!StringUtil.checkObj(json)){&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>&nbsp;</span><span class="keyword">null</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">try</span><span>&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,&nbsp;<span class="keyword">false</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DateFormat&nbsp;myDateFormat&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;SimpleDateFormat(</span><span class="string">"yyyy-MM-dd&nbsp;HH:mm:ss"</span><span>);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mapper.getSerializationConfig().setDateFormat(myDateFormat);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>&nbsp;(T)mapper.readValue(json,&nbsp;clazz);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<span class="keyword">catch</span><span>&nbsp;(IOException&nbsp;e)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;log.error(e);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">throw</span><span>&nbsp;</span><span class="keyword">new</span><span>&nbsp;IllegalArgumentException(e);&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li></ol></div><pre class="java" name="code" style="display: none;">public static &lt;T&gt; T renderJson2Object(String json, Class clazz){ if(!StringUtil.checkObj(json)){ return null; } try { mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false); DateFormat myDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); mapper.getSerializationConfig().setDateFormat(myDateFormat); return (T)mapper.readValue(json, clazz); } catch (IOException e) { log.error(e); throw new IllegalArgumentException(e); } }</pre> <p>&nbsp;,這個方法能夠根據json字符串,轉換爲clazz指定的類型的對象,如:</p> <div class="dp-highlighter" id=""><div class="bar"><div class="tools">Java代碼 <embed wmode="transparent" src="/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=renderJson2Object(%22%7B%5C%22name%5C%22%3A%5C%22china%5C%22%2C%5C%22age%5C%22%3A%5C%22%3Cspan%20style%3D%22white-space%3A%20normal%3B%22%3E5000%3C%2Fspan%3E%5C%22%7D%22%2CStudent.class)%3B" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="javascript:void()" title="收藏這段代碼" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="/images/icon_star.png" alt="收藏代碼"><img class="spinner" src="/images/spinner.gif" style="display:none"></a></div></div><ol start="1" class="dp-j"><li><span><span>renderJson2Object(</span><span class="string">"{\"name\":\"china\",\"age\":\"&lt;span&nbsp;style="</span><span>white-space:&nbsp;normal;</span><span class="string">"&gt;5000&lt;/span&gt;\"}"</span><span>,Student.</span><span class="keyword">class</span><span>);&nbsp;&nbsp;</span></span></li></ol></div><pre class="java" name="code" style="display: none;">renderJson2Object("{\"name\":\"china\",\"age\":\"<span style="white-space: normal;">5000</span>\"}",Student.class);</pre> <p>Student類裏有name,age的get,set方法,代碼以下:</p> <p>&nbsp;</p> <div class="dp-highlighter" id=""><div class="bar"><div class="tools">Java代碼 <embed wmode="transparent" src="/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=public%20class%20Student%20implements%20Serializable%7B%0A%20%20%20%20private%20static%20final%20long%20serialVersionUID%20%3D%20685922460589405829L%3B%0A%0A%20%20%20%20private%20String%20name%3B%0A%20%20%20%20private%20String%20age%3B%0A%0A%20%20%20%2F*get%20set%E7%95%A5*%2F%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="javascript:void()" title="收藏這段代碼" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="/images/icon_star.png" alt="收藏代碼"><img class="spinner" src="/images/spinner.gif" style="display:none"></a></div></div><ol start="1" class="dp-j"><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;Student&nbsp;</span><span class="keyword">implements</span><span>&nbsp;Serializable{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">final</span><span>&nbsp;</span><span class="keyword">long</span><span>&nbsp;serialVersionUID&nbsp;=&nbsp;685922460589405829L;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;String&nbsp;name;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;String&nbsp;age;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;<span class="comment">/*get&nbsp;set略*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre class="java" name="code" style="display: none;">public class Student implements Serializable{ private static final long serialVersionUID = 685922460589405829L;javascript

private String name;
private String age;

/get set略/ }</pre>php

<p>&nbsp;</p> <p>根據上面的json字符串能夠正常轉換爲Student對象,</p> <p>可是一般狀況下,從前端傳json字符串到後端,json字符串的值是不可控的或者被框架修改了json字符串,如在裏面添加了其餘的鍵值對,</p> <p>如如今的json字符串爲:"{<span style="background-color: #ff0000;">\"address\":\"hunan\",</span>\"name\":\"<span style="white-space: pre;">china</span>\",\"age\":\"5000\"}",</p> <p>Student類里根本沒有address屬性,這樣的狀況下使用<span style="white-space: pre;">renderJson2Object方法時,會拋</span></p> <div class="dp-highlighter" id=""><div class="bar"><div class="tools">Java代碼 <embed wmode="transparent" src="/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=java.lang.IllegalArgumentException%3A%20org.codehaus.jackson.map.exc.UnrecognizedPropertyException%3A%20Unrecognized%20field%20%22address%22%20(Class%20util.Student)%2C%20not%20marked%20as%20ignorable" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="javascript:void()" title="收藏這段代碼" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="/images/icon_star.png" alt="收藏代碼"><img class="spinner" src="/images/spinner.gif" style="display:none"></a></div></div><ol start="1" class="dp-j"><li><span><span>java.lang.IllegalArgumentException:&nbsp;org.codehaus.jackson.map.exc.UnrecognizedPropertyException:&nbsp;Unrecognized&nbsp;field&nbsp;</span><span class="string">"address"</span><span>&nbsp;(Class&nbsp;util.Student),&nbsp;not&nbsp;marked&nbsp;as&nbsp;ignorable&nbsp;&nbsp;</span></span></li></ol></div><pre class="java" name="code" style="display: none;">java.lang.IllegalArgumentException: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "address" (Class util.Student), not marked as ignorable</pre> <p>&nbsp;&nbsp;意思是說Student類裏沒有<span style="white-space: pre;">address這個屬性,因此沒法正常轉化,同時還指明瞭</span><span style="white-space: pre;">not marked as ignorable,即沒有標明可忽略的特性,先看源碼這句話的理解這句話的意思</span></p> <p>類:org.codehaus.jackson.map.deser.BeanDeserializer中的</p> <p>&nbsp;</p> <div class="dp-highlighter" id=""><div class="bar"><div class="tools">Java代碼 <embed wmode="transparent" src="/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=%40Override%0A%20%20%20%20protected%20void%20handleUnknownProperty(JsonParser%20jp%2C%20DeserializationContext%20ctxt%2C%20Object%20beanOrClass%2C%20String%20propName)%0A%20%20%20%20%20%20%20%20throws%20IOException%2C%20JsonProcessingException%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20...%20...%20...%0A%20%20%20%20%20%20%20%20%3Cspan%20style%3D%22color%3A%20%23ff0000%3B%22%3E%2F%2F%20If%20registered%20as%20ignorable%2C%20skip%3C%2Fspan%3E%0A%20%20%20%20%20%20%20%20%3Cspan%20style%3D%22color%3A%20%23ff0000%3B%22%3Eif%20(_ignoreAllUnknown%20%7C%7C%0A%20%20%20%20%20%20%20%20%20%20%20%20(_ignorableProps%20!%3D%20null%20%26%26%20_ignorableProps.contains(propName)))%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20jp.skipChildren()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%20%20%20%20%7D%3C%2Fspan%3E%0A%20%20%20%20%20%20%20%20%20...%20...%20...%0A%20%20%20%20%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="javascript:void()" title="收藏這段代碼" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="/images/icon_star.png" alt="收藏代碼"><img class="spinner" src="/images/spinner.gif" style="display:none"></a></div></div><ol start="1" class="dp-j"><li><span><span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;handleUnknownProperty(JsonParser&nbsp;jp,&nbsp;DeserializationContext&nbsp;ctxt,&nbsp;Object&nbsp;beanOrClass,&nbsp;String&nbsp;propName)&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">throws</span><span>&nbsp;IOException,&nbsp;JsonProcessingException&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;...&nbsp;...&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;span&nbsp;style=<span class="string">"color:&nbsp;#ff0000;"</span><span>&gt;</span><span class="comment">//&nbsp;If&nbsp;registered&nbsp;as&nbsp;ignorable,&nbsp;skip&lt;/span&gt;</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;span&nbsp;style=<span class="string">"color:&nbsp;#ff0000;"</span><span>&gt;</span><span class="keyword">if</span><span>&nbsp;(_ignoreAllUnknown&nbsp;||&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(_ignorableProps&nbsp;!=&nbsp;<span class="keyword">null</span><span>&nbsp;&amp;&amp;&nbsp;_ignorableProps.contains(propName)))&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jp.skipChildren();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&lt;/span&gt;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;...&nbsp;...&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li></ol></div><pre class="java" name="code" style="display: none;">@Override protected void handleUnknownProperty(JsonParser jp, DeserializationContext ctxt, Object beanOrClass, String propName) throws IOException, JsonProcessingException { ... ... ... <span style="color: #ff0000;">// If registered as ignorable, skip</span> <span style="color: #ff0000;">if (_ignoreAllUnknown || (_ignorableProps != null &amp;&amp; _ignorableProps.contains(propName))) { jp.skipChildren(); return; }</span> ... ... ... }</pre> <p>&nbsp;&nbsp; &nbsp;源碼註釋說,若是註冊了忽略特性,則會跳過此步驟,那到底須要怎麼忽略呢?</p> <p>請再看類:org.codehaus.jackson.map.deser.BeanDeserializerFactory中的</p> <p>&nbsp;</p> <div class="dp-highlighter" id=""><div class="bar"><div class="tools">Java代碼 <embed wmode="transparent" src="/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=protected%20void%20addBeanProps(DeserializationConfig%20config%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20BasicBeanDescription%20beanDesc%2C%20BeanDeserializerBuilder%20builder)%0A%20%20%20%20%20%20%20%20throws%20JsonMappingException%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20...%20....%20...%0A%20%20%20%20%20%20%20%20%2F%2F%20Things%20specified%20as%20%22ok%20to%20ignore%22%3F%20%5BJACKSON-77%5D%0A%20%20%20%20%20%20%20%20AnnotationIntrospector%20intr%20%3D%20config.getAnnotationIntrospector()%3B%0A%20%20%20%20%20%20%20%20boolean%20ignoreAny%20%3D%20false%3B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20Boolean%20B%20%3D%20intr.findIgnoreUnknownProperties(beanDesc.getClassInfo())%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(B%20!%3D%20null)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ignoreAny%20%3D%20B.booleanValue()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20builder.setIgnoreUnknownProperties(ignoreAny)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20...%20...%20...%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="javascript:void()" title="收藏這段代碼" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="/images/icon_star.png" alt="收藏代碼"><img class="spinner" src="/images/spinner.gif" style="display:none"></a></div></div><ol start="1" class="dp-j"><li><span><span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;addBeanProps(DeserializationConfig&nbsp;config,&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BasicBeanDescription&nbsp;beanDesc,&nbsp;BeanDeserializerBuilder&nbsp;builder)&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">throws</span><span>&nbsp;JsonMappingException&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;....&nbsp;...&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//&nbsp;Things&nbsp;specified&nbsp;as&nbsp;"ok&nbsp;to&nbsp;ignore"?&nbsp;[JACKSON-77]</span><span>&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AnnotationIntrospector&nbsp;intr&nbsp;=&nbsp;config.getAnnotationIntrospector();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">boolean</span><span>&nbsp;ignoreAny&nbsp;=&nbsp;</span><span class="keyword">false</span><span>;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Boolean&nbsp;B&nbsp;=&nbsp;intr.findIgnoreUnknownProperties(beanDesc.getClassInfo());&nbsp;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">if</span><span>&nbsp;(B&nbsp;!=&nbsp;</span><span class="keyword">null</span><span>)&nbsp;{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignoreAny&nbsp;=&nbsp;B.booleanValue();&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;builder.setIgnoreUnknownProperties(ignoreAny);&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;...&nbsp;...&nbsp;&nbsp;</span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre class="java" name="code" style="display: none;">protected void addBeanProps(DeserializationConfig config, BasicBeanDescription beanDesc, BeanDeserializerBuilder builder) throws JsonMappingException { ... .... ... // Things specified as "ok to ignore"? [JACKSON-77] AnnotationIntrospector intr = config.getAnnotationIntrospector(); boolean ignoreAny = false; { Boolean B = intr.findIgnoreUnknownProperties(beanDesc.getClassInfo()); if (B != null) { ignoreAny = B.booleanValue(); builder.setIgnoreUnknownProperties(ignoreAny); } } ... ... ... }</pre> <p>&nbsp;</p> <p>&nbsp;<span style="white-space: pre;">intr.findIgnoreUnknownProperties(beanDesc.getClassInfo()); </span></p> <p><span style="white-space: pre;"><span style="color: #ff0000;">會查找目標對象中,是否使用了JsonIgnoreProperties 註解,其中把註解的value值賦給了</span></span><span style="white-space: pre;">builder.setIgnoreUnknownProperties(ignoreAny);</span></p> <p>到此Student類的正確作法爲:</p> <p>&nbsp;</p> <div class="dp-highlighter" id=""><div class="bar"><div class="tools">Java代碼 <embed wmode="transparent" src="/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=%3Cspan%20style%3D%22color%3A%20%23ff0000%3B%22%3E%40JsonIgnoreProperties(ignoreUnknown%20%3D%20true)%20%3C%2Fspan%3E%0Apublic%20class%20Student%20implements%20Serializable%7B%0A%20%20%20%20private%20static%20final%20long%20serialVersionUID%20%3D%20685922460589405829L%3B%0A%0A%20%20%20%20private%20String%20name%3B%0A%20%20%20%20private%20String%20age%3B%0A%0A%20%20%20%2F*get%20set.....*%2F%0A%EF%BD%9D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="javascript:void()" title="收藏這段代碼" onclick="code_favorites_do_favorite(this);return false;"><img class="star" src="/images/icon_star.png" alt="收藏代碼"><img class="spinner" src="/images/spinner.gif" style="display:none"></a></div></div><ol start="1" class="dp-j"><li><span><span>&lt;span&nbsp;style=</span><span class="string">"color:&nbsp;#ff0000;"</span><span>&gt;</span><span class="annotation">@JsonIgnoreProperties</span><span>(ignoreUnknown&nbsp;=&nbsp;</span><span class="keyword">true</span><span>)&nbsp;&lt;/span&gt;&nbsp;&nbsp;</span></span></li><li><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;Student&nbsp;</span><span class="keyword">implements</span><span>&nbsp;Serializable{&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;</span><span class="keyword">static</span><span>&nbsp;</span><span class="keyword">final</span><span>&nbsp;</span><span class="keyword">long</span><span>&nbsp;serialVersionUID&nbsp;=&nbsp;685922460589405829L;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;String&nbsp;name;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">private</span><span>&nbsp;String&nbsp;age;&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;<span class="comment">/*get&nbsp;set.....*/</span><span>&nbsp;&nbsp;</span></span></li><li><span>}&nbsp;&nbsp;</span></li></ol></div><pre class="java" name="code" style="display: none;"><span style="color: #ff0000;">@JsonIgnoreProperties(ignoreUnknown = true) </span> public class Student implements Serializable{ private static final long serialVersionUID = 685922460589405829L;css

private String name;
private String age;

/get set...../ }</pre>  看紅色註解,如今暫時找到在類中添加註解(感受具體的pojo對象和jackson耦合),不知道有沒有其餘方法,設全局變量來控制,若是有朋友知道,請告訴我謝謝。。。前端

<p>&nbsp;</p> <p>&nbsp;</p> <p>謝謝&nbsp;<a target="_blank" style="background-color: #effaff; font-family: Verdana, Arial, Helvetica, sans-serif; color: #108ac6; line-height: 18px; text-align: left;" href="http://up2pu.iteye.com/">up2pu</a><span style="background-color: #effaff; font-family: Verdana, Arial, Helvetica, sans-serif; line-height: 18px; text-align: left;">&nbsp; 兄弟的幫助,使用</span><span style="line-height: 18px; text-align: left; font-family: Verdana, Arial, Helvetica, sans-serif;">mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false),</span></p> <p><span style="color: #ff0000; font-family: Verdana, Arial, Helvetica, sans-serif;">則無需在目標類中添加JsonIgnoreProperties註解</span></p> </div> <p>&nbsp;</p>java

<div> <!-- 廣告位:PC端-iteye博客詳情頁底部banner2-728*90 --> <script> (function() { var s = "_" + Math.random().toString(36).slice(2); document.write('<div id="' + s + '"></div>'); (window.slotbydup=window.slotbydup || []).push({ id: '4774193', container: s, size: '728,90', display: 'inlay-fix' }); })(); jQuery(function(){ window.csdn.recommendSide({ contentBox: "recommend", postBox: jQuery(".news-right-side"), query:'jackson 的UnrecognizedPropertyException錯誤', popu:'725' }) }); </script><div id="_7gng1qfoi9j"></div><script charset="utf-8" src="https://pos.baidu.com/sctm?psi=17b2271768ad8e572de189b1ca12f21b&amp;di=4774193&amp;dri=0&amp;dis=0&amp;dai=0&amp;ps=2711x580&amp;enu=encoding&amp;dcb=___adblockplus&amp;dtm=SSP_JSONP&amp;dvi=0.0&amp;dci=-1&amp;dpt=none&amp;tsr=0&amp;tpr=1559033834734&amp;ti=jackson%20%E7%9A%84UnrecognizedPropertyException%E9%94%99%E8%AF%AF%20-%20olivechinese%20-%20IT&amp;ari=2&amp;dbv=2&amp;drs=1&amp;pcs=1903x947&amp;pss=1903x2759&amp;cfv=0&amp;cpl=3&amp;chi=1&amp;cce=true&amp;cec=UTF-8&amp;tlm=1559033834&amp;prot=2&amp;rw=947&amp;ltu=https%3A%2F%2Folivechinese.iteye.com%2Fblog%2F1131059&amp;ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DS5DBbjZqbXjj1EDOtw9NQxH4_h8WFG_rObT2Wa0AssFZikeDvAs83zKAjPDC4YRyWWaNXFxIgt4gjFRt3Bi57a%26wd%3D%26eqid%3De41ee3ec00307f80000000035cecf6b4&amp;lcr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DS5DBbjZqbXjj1EDOtw9NQxH4_h8WFG_rObT2Wa0AssFZikeDvAs83zKAjPDC4YRyWWaNXFxIgt4gjFRt3Bi57a%26wd%3D%26eqid%3De41ee3ec00307f80000000035cecf6b4&amp;ecd=1&amp;uc=1920x1050&amp;pis=-1x-1&amp;sr=1920x1080&amp;tcn=1559033835&amp;exps=110011"></script><a href="http://www.baidu.com/cb.php?c=IgF_pyfqnHmkPjbvrHT0IZ0qnfK9ujYzrH6YrjmY0Aw-5Hnsn16YnjT0TAq15HfLP1fkrHn0T1dhmyR1nyfYPHPWPvwhnHR30AwY5HDdPHbsn1n3n1R0IgF_5y9YIZK1rBtEuywdQhP1uA38UhNYQLwETA-WQvG9IhDk5LNYUNq1ULNzmvRqmhkEuv-Yug--0ZFb5HD0mhYqnfKsTWYs0ZNGujYkPHTYn1mk0AqGujYknWb3rjDY0APGujYLn1bdn1n0ULI85H00TZbqnW0v0APzm1YYnj01rf" target="_blank" style="text-decoration:none;"> <div style="font-size:17px;color:#f13d3d;font-family: SF Pro Display,Roboto,Noto,Arial,PingFang SC,Hiragino Sans GB,Microsoft YaHei,sans-serif;width:728px;height:25px;font-weight:bold;">資深Java項目團隊歷時1年打造,實戰精髓大揭祕!</div> <div style="font-size:13px;color:#666;font-family: SF Pro Display,Roboto,Noto,Arial,PingFang SC,Hiragino Sans GB,Microsoft YaHei,sans-serif;height:35px;margin-top:10px;">Java系統學習全案:五大階段學習系統規劃、8大企業級項目貫穿全程。限時2折秒殺。立省4688元!</div>json

</a> </div>後端

</div>app

<div class="blog_comment"> <h5>發表評論</h5> <form action="/blog/1131059" id="comment_form" method="post" onsubmit="return false;"><div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="5U/ymtJOL+iJ3sxZv5yTDpDB4MOefG1N5vG+gntGvVo="></div> 框架

<input type="hidden" id="editor_bbcode_flag" value="true">dom

</div>

<script type="text/javascript"> var editor = new Control.TextArea.Editor("editor_body", "bbcode", false); </script>

<p style="text-align:right;margin-right:30px;">(快捷鍵 Alt+S / Ctrl+Enter) <input class="submit" id="quick_reply_button" name="commit" type="submit" value="提交"></p>
   </form>
    <script type="text/javascript">
      new HotKey("s",function() {$('quick_reply_button').click();},{altKey: true, ctrlKey: false});
      new HotKey(new Number(13),function() {$('quick_reply_button').click();},{altKey: false, ctrlKey: true});

      new Validation("comment_form", {immediate: false, onFormValidate: function(result, form){
        if(result) {
          new Ajax.Request('/blog/create_comment/1131059', {
            onFailure:function(response){
              $('comments').insert({after:response.responseText})
              form.spinner.hide();
              Element.scrollTo($('comments'));
            },
            onSuccess:function(response){
              Element.scrollTo($('comments'));
              var new_comment = new Element('div', {}).update(response.responseText).firstChild;
              var comment_id = new_comment.readAttribute('id');

              $('comments').insert({after:response.responseText});
              $('editor_body').value = "";

              var css_rules = '#' + comment_id + ' pre';
              highlightNewAddContent(css_rules);
              processComment();
              code_favorites_init(css_rules);
              
              form.spinner.hide();
            }, parameters:Form.serialize(form)
          });
        }
    }});
    </script>
    </div>

</div>

相關文章
相關標籤/搜索