如题

通过 net.sf.json.JSONObject.toBean 将Json 转换成 Java Bean 时报警,调用一次的警告信息还挺多。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
16:08:45,098 <net.sf.ezmorph.bean.BeanMorpher>  WARN [pool-2-thread-1]: Property 'java.lang.String.date' does not exist. SKIPPED.
16:08:45,098 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.hours' does not exist. SKIPPED.
16:08:45,098 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.seconds' does not exist. SKIPPED.
16:08:45,098 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.month' does not exist. SKIPPED.
16:08:45,098 <net.sf.ezmorph.bean.BeanMorpher> INFO [pool-2-thread-1]: Property 'java.util.Date.timezoneOffset' has no write method. SKIPPED.
16:08:45,099 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.year' does not exist. SKIPPED.
16:08:45,099 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.minutes' does not exist. SKIPPED.
16:08:45,099 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.time' does not exist. SKIPPED.
16:08:45,099 <net.sf.ezmorph.bean.BeanMorpher> INFO [pool-2-thread-1]: Property 'java.util.Date.class' has no write method. SKIPPED.
16:08:45,099 <net.sf.ezmorph.bean.BeanMorpher> INFO [pool-2-thread-1]: Property 'java.util.Date.day' has no write method. SKIPPED.
16:08:45,115 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.date' does not exist. SKIPPED.
16:08:45,115 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.hours' does not exist. SKIPPED.
16:08:45,115 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.seconds' does not exist. SKIPPED.
16:08:45,116 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.month' does not exist. SKIPPED.
16:08:45,116 <net.sf.ezmorph.bean.BeanMorpher> INFO [pool-2-thread-1]: Property 'java.util.Date.timezoneOffset' has no write method. SKIPPED.
16:08:45,116 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.year' does not exist. SKIPPED.
16:08:45,116 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.minutes' does not exist. SKIPPED.
16:08:45,116 <net.sf.ezmorph.bean.BeanMorpher> WARN [pool-2-thread-1]: Property 'java.lang.String.time' does not exist. SKIPPED.
16:08:45,116 <net.sf.ezmorph.bean.BeanMorpher> INFO [pool-2-thread-1]: Property 'java.util.Date.class' has no write method. SKIPPED.
16:08:45,116 <net.sf.ezmorph.bean.BeanMorpher> INFO [pool-2-thread-1]: Property 'java.util.Date.day' has no write method. SKIPPED.

Json-lib maven 依赖配置

1
2
3
4
5
6
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>

Json-lib 官网http://json-lib.sourceforge.net/
Json-lib 是很老的工具了,最新的 2.4 版本还是2010年发布的,这里的classifier配置必须有,否则依赖异常。

java 代码

我自己对该功能作了一次封装,之前的代码如下

1
2
3
4
5
6
7
/**
* 将JSON转换成POJO,其中beanClz为POJO的Class
*/
public static <T> T json2Object(String json, Class beanClz)
{
return (T) JSONObject.toBean(JSONObject.fromObject(json), beanClz);
}

解决办法:在 JSONObject.toBean 之前添加 registerMorpher 方法,配置日期格式

1
2
3
4
5
6
7
8
/**
* 将JSON转换成POJO,其中beanClz为POJO的Class
*/
public static <T> T json2Object(String json, Class beanClz)
{
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"yyyy-MM-dd HH:mm:ss"}));
return (T) JSONObject.toBean(JSONObject.fromObject(json), beanClz);
}

再查看运行日志,一下子清爽多了,之前满屏都是 …SKIPPED