异常:
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:
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;
}
}
测试程序:
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());
}
}
结果就报了以上的错误:
java.lang.NoSuchMethodException: Unknown property XXXX
具体解决,如下代码:
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());
}
}
总结:
config配置项的作用就是过滤属性,出现这个Exception的原因有两点:
1)Json字符串属性过多,没有对应上javaBean的属性,所以程序会抛异常没有对应的属性。(使用gson不会出错)。
2)使用JsonConfig配置项过滤掉多余的属性。注意,是在字符串转为JSONObject的时候过滤。
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);
否则又报错:
类转换异常
来源:oschina
链接:https://my.oschina.net/u/1989321/blog/368116