異常:java
net.sf.json.JSONException: java.lang.NoSuchMethodException: Unknown property 'success' on class 'class com.leagsoft.restservice.common.bean.Message' at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy.setProperty(PropertySetStrategy.java:45) at net.sf.json.JSONObject.setProperty(JSONObject.java:1473) at net.sf.json.JSONObject.toBean(JSONObject.java:474) at net.sf.json.JSONObject.toBean(JSONObject.java:249) at com.leagsoft.restservice.json.JsonTest.testJson2(JsonTest.java:20) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) Caused by: java.lang.NoSuchMethodException: Unknown property 'success' on class 'class com.leagsoft.restservice.common.bean.Message' at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2123) at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:890) at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy.setProperty(PropertySetStrategy.java:43) ... 27 more
javaBean:apache
package com.leagsoft.restservice.common.bean; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import com.leagsoft.restservice.util.MessageUtil; /** * * @author Heweipo * */ @XmlRootElement(name="message") // 標註類名爲XML根節點 @XmlAccessorType(XmlAccessType.FIELD) // 表示將全部域做爲XML節點 public class Message { /** * 信息獲取的狀態碼 */ private int status; /** * 信息獲取的提示消息 */ private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } }
測試程序:json
public class JsonTest { @Test public void testJson2(){ String json = "{'message':{'status':6000,'msg':'未知異常','success':'true'}}"; Message message = (Message)JSONObject.toBean(JSONObject.fromObject(json).getJSONObject("message"),Message.class); System.out.println(message.getStatus()); } }
結果就報了以上的錯誤:app
java.lang.NoSuchMethodException: Unknown property XXXXeclipse
具體解決,以下代碼:測試
public class JsonTest { @Test public void testJson2(){ String json = "{'message':{'status':6000,'msg':'未知異常','success':'true'}}"; JsonConfig config = new JsonConfig(); config.setJavaPropertyFilter(new PropertyFilter(){ public boolean apply(Object paramObject1, String paramString, Object paramObject2) { if(paramString.equals("status")||paramString.equals("msg")){ return false; } return true; }}); Message message = (Message)JSONObject.toBean(JSONObject.fromObject(JSONObject.fromObject(json).getString("message"),config),Message.class); System.out.println(message.getStatus()); } }
總結:this
config配置項的做用就是過濾屬性,出現這個Exception的緣由有兩點:lua
1)Json字符串屬性過多,沒有對應上javaBean的屬性,因此程序會拋異常沒有對應的屬性。(使用gson不會出錯)。spa
2)使用JsonConfig配置項過濾掉多餘的屬性。注意,是在字符串轉爲JSONObject的時候過濾。rest
3)JSONObject.toBean能夠接收三個參數,這個接收三個參數的方法不要使用,會報錯,而要使用接收兩個參數的方法,另外,屬性的過濾必定是在轉爲JSONObject的時候,而不是轉Bean的時候。
4)JSONObject.fromObject(JSONObject.fromObject(result).getString("message"),config) 字符串轉爲JSONObject的時候過濾
5)(Message)JSONObject.toBean(JSONObject.fromObject(JSONObject.fromObject(result).getString("message"),config),Message.class); 不能使用(Message)JSONObject.toBean(JSONObject.fromObject(JSONObject.fromObject(result).getString("message"),config),Message.class,config);
不然又報錯:
類轉換異常