The server encountered an internal error that prevented it from fulfilling this request.(JsonMapping

在測試一個方法,dubug查看查詢結果已經出來了,結果頁面上是The server encountered an internal error that prevented it from fulfilling this request.,再debug ,發現ServletInvocableHandlerMethod#invokeAndHandle中 發現以下異常:spring

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Conflicting getter definitions for property "buyNow":..........nested exception is org.codehaus.jackson.map.JsonMappingException: Conflicting getter definitions for property "buyNow".....xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#isBuyNow(0 params) vs xxxxxxxxxxxxxxxx#getBuyNow(0 params)
從上面的異常找到相應的bean,以下圖3行和11行
 1 /** 是否直接購買(1|是,0|不是,默認0) */
 2 private Integer buyNow;
 3 public Integer getBuyNow (){
 4     return buyNow;
 5 }
 6 
 7 public void setBuyNow (Integer buyNow){
 8     this.buyNow = buyNow;
 9 }
10 
11 public boolean isBuyNow() {
12     return BUY_NOW_YES.equals(buyNow);
13 }

從異常提示的信息能夠看出jackson處理bean時不知道該調用#isBuyNow仍是#getBuyNow,對於這次固然能夠告訴jackson忽略,加個@JsonIgnore是能夠的,可是這個地方其實平時代碼規範的問題,其實不只是jackson處理上還有其它的地方也會有此問題,因此方法命名上就要杜絕這種is打頭的命名方式是最好的(我認爲的)。app

測試

相關文章
相關標籤/搜索