Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

json 转java 的时候,想排除某个属性 #3

Closed
venusdrogon opened this issue Jan 20, 2018 · 1 comment
Closed

json 转java 的时候,想排除某个属性 #3

venusdrogon opened this issue Jan 20, 2018 · 1 comment
Assignees
Milestone

Comments

@venusdrogon
Copy link
Collaborator

venusdrogon commented Jan 20, 2018

json 转java 的时候,想排除某个属性

比如 taobao 接口返回的数据, 有老的废弃属性 county (可能早期由于单词写错了. 为了兼容老代码)

{
            "country": "中国",
            "country_id": "CN",
            "area": "华北",
            "area_id": "100000",
            "region": "北京市",
            "region_id": "110000",
            "city": "北京市",
            "city_id": "110100",
            "county": "",
            "county_id": "-1",
            "isp": "科技网",
            "isp_id": "1000114",
            "ip": "210.75.225.254"
        }

但是我们的java bean 没有这个属性

导致转换的时候, 由于 使用了 PropertyStrategyWrapper

  public void setProperty(Object bean,String key,Object value){
        try{
            propertySetStrategy.setProperty(bean, key, value);
        }catch (Exception e){
            LOGGER.warn(e.getMessage(), e);
        }
    }

会提示 warn 级别的日志, 很烦人


20:33:38 WARN  (PropertyStrategyWrapper.java:69) [setProperty()] java.lang.NoSuchMethodException: Unknown property 'county' on class 'class com.feilong.tools.IpInfoEntity'
net.sf.json.JSONException: java.lang.NoSuchMethodException: Unknown property 'county' on class 'class com.feilong.tools.IpInfoEntity'
	at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy._setProperty(PropertySetStrategy.java:69)
	at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy.setProperty(PropertySetStrategy.java:60)
	at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy.setProperty(PropertySetStrategy.java:45)
	at com.feilong.json.jsonlib.PropertyStrategyWrapper.setProperty(PropertyStrategyWrapper.java:67)
	at net.sf.json.util.PropertySetStrategy.setProperty(PropertySetStrategy.java:40)
	at net.sf.json.JSONObject.setProperty(JSONObject.java:1394)
	at net.sf.json.JSONObject.toBean(JSONObject.java:429)
	at com.feilong.json.jsonlib.JsonUtil.toBean(JsonUtil.java:1275)
	at com.feilong.tools.IpUtil.toIpInfoEntity(IpUtil.java:162)
	at com.feilong.tools.IpUtil.getIpInfoEntity(IpUtil.java:138)
	at com.feilong.tools.IpUtilTest.test(IpUtilTest.java:36)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.NoSuchMethodException: Unknown property 'county' on class 'class com.feilong.tools.IpInfoEntity'
	at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2091)
	at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:928)
	at net.sf.json.util.PropertySetStrategy$DefaultPropertySetStrategy._setProperty(PropertySetStrategy.java:67)
	... 33 more

@venusdrogon venusdrogon added this to the 1.11.0 milestone Jan 20, 2018
@venusdrogon venusdrogon self-assigned this Jan 20, 2018
@venusdrogon
Copy link
Collaborator Author

新建 ArrayExcludePropertyNamesPropertyFilter

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant