版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/42613325
警告: Can't transform property 'acceptEvent' from java.lang.String into java.util.List. Will register a default Morpher
产生错误的代码:
import net.sf.json.JSONObject;
public static <T> T jsonToObject(String jsonString, Class<T> pojoCalss) {
try{
Object pojo;
JSONObject jsonObject = JSONObject.fromObject(jsonString);
pojo = JSONObject.toBean(jsonObject, pojoCalss);
@SuppressWarnings("unchecked")
T t = (T)pojo;
return t;
}catch(Exception ex){
return null;
}
}
需要实例化的实体类如下:
import java.io.Serializable;
import java.util.List;
/**
* 传送的消息数据对象
*
* @author testcs_dn
*/
public final class Data implements Serializable {
/**
*
*/
private static final long serialVersionUID = 387186819023110091L;
// 客户端id
private String id;
// 客户端ip
private String ip;
// 客户端过期时间
private int clientTimeOut = 30 * 1000;
// 服务端过期时间
private int serverTimeOut = 60 * 1000;
// 传送数据
private Object data;
// 时间戳
private long timeStamp;
// 消息类型
private String operate;
// 消息返回状态(未处理,已处理(成功、失败))
private String state;
// 客户端接收的事件类型
private List<String> acceptEvent;
public Data() {
}
public String getOperate() {
return operate;
}
public void setOperate(String operate) {
this.operate = operate;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public long getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(long timeStamp) {
this.timeStamp = timeStamp;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getClientTimeOut() {
return clientTimeOut;
}
public void setClientTimeOut(int clientTimeOut) {
this.clientTimeOut = clientTimeOut;
}
public int getServerTimeOut() {
return serverTimeOut;
}
public void setServerTimeOut(int serverTimeOut) {
this.serverTimeOut = serverTimeOut;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
@Override
public String toString() {
return "Data [id=" + id + ", ip=" + ip + ", clientTimeOut="
+ clientTimeOut + ", serverTimeOut=" + serverTimeOut
+ ", data=" + data + ", timeStamp=" + timeStamp + ", operate="
+ operate + ", state=" + state + ", acceptEvent=" + acceptEvent
+ "]";
}
public List<String> getAcceptEvent() {
return acceptEvent;
}
public void setAcceptEvent(List<String> acceptEvent) {
this.acceptEvent = acceptEvent;
}
}
解决方案:
参考:http://www.iteye.com/problems/675
需要使用JSONUtils.getMorpherRegistry().registerMorpher( );注册对应的Morpher,但研究了一番还没搞清楚自定义的Morpher怎么写。