带有反序列化列表对象的 Spring 控制器
@RequestMapping(value = "/tester/", method = RequestMethod.POST)
public String testingonly(@RequestBody List<EachObject> eachobjectlist) throws IOException {
然后我迭代eachobjectlist,但列表中的每个项目都是LinkedHashMap类型,它不是应该是“EachObject”类型吗?
@RequestMapping(value = "/tester/", method = RequestMethod.POST)
public String testingonly(@RequestBody List<EachObject> eachobjectlist) throws IOException {
then i do iterate eachobjectlist, but each item inside the list is type LinkedHashMap, arent it suppose to be "EachObject" type ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于运行时类型擦除,泛型类型EachObject
在运行时不存在。 Spring 将无法确定此信息。 Spring只知道参数有一个原始类型List
,并且它将注入任何类型的List
。根据@axtavt,方法参数的泛型类型是可以访问,但是Spring可能没有实现这个,并且将来可能不会实现。这是因为泛型类型可以是接口或抽象类,Spring 将无法创建给定接口或抽象类的相应实现。例如,如果您请求
Fruit
列表,并且有两个几乎相同的Fruit
子类,则无法确定要使用哪个子类。Due to type erasure at runtime, the generic typeEachObject
is not present at runtime. Spring will not be able to determine this information. Spring only knows that the argument has a raw typeList
and it will inject aList
of any type.According to @axtavt, generic types of method's arguments are accessible but Spring may not have this implemented and probably won't be implemented in the future. This is because generic type can be interface or abstract class and Spring will not be able to create a corresponding implementation of a given interface or abstract class. For example, if you request for a list of
Fruit
and there are two almost identical subclasses ofFruit
, dertemining which subclass to be used will not be possible.