【问题标题】:why java returns java.util.LinkedHashMap when ... complex为什么当...复杂时java返回java.util.LinkedHashMap
【发布时间】:2013-08-08 16:14:53
【问题描述】:

我的代码如下...

@ResponseBody 
@RequestMapping(value ="/updateAll", method=RequestMethod.POST)  
public JSONObject update( @RequestBody List<T> list){ 
    try{
        T entity = list.get(0);
        System.out.println(entity.getClass());
        dbService.update(entityName + ".update", list);
        return jsonResult(null, null, null, 0);
    }catch(Exception ex){
        return jsonResult(null, null, ex, 0);
    }
}

当我通过@RequestBody 只获取一个实体时,它返回一个正确的对象名称,但上面的代码返回 java.util.LinkedHashMap,我无法使用 iBatis 中 parameterClass 的优点。 (对不起我的英语不好......)

我再次将列表转换为 T ,但它仍然返回 LinkedHashMap。

不是代码 [ T entity = list.get(0); ] 表示它将列表识别为对象 T 的集合?

@ResponseBody 
@RequestMapping(value ="/updateAll", method=RequestMethod.POST)  
public JSONObject update( @RequestBody T entity){ 
    try{
        System.out.println(entity.getClass());
        dbService.update(entityName + ".update", entity);
        return jsonResult(null, null, null, 0);
    }catch(Exception ex){
        return jsonResult(null, null, ex, 0);
    }
}

这会毫无问题地准确打印实体名称。

谁能解释一下为什么会这样?我可以在哪里了解更多信息?

【问题讨论】:

    标签: java list generics collections casting


    【解决方案1】:
     /**
         * Returns the runtime class of this {@code Object}. The returned
         * {@code Class} object is the object that is locked by {@code
         * static synchronized} methods of the represented class.
         *
         * <p><b>The actual result type is {@code Class<? extends |X|>}
         * where {@code |X|} is the erasure of the static type of the
         * expression on which {@code getClass} is called.</b> For
         * example, no cast is required in this code fragment:</p>
         *
         * <p>
    public final native Class<?> getClass();
    

    引用类型是什么并不重要。方法getClass() 返回runtime class

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-29
      • 2023-02-01
      • 1970-01-01
      • 2011-05-22
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      相关资源
      最近更新 更多