欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springmvc @RequestBody绑定泛型集合

程序员文章站 2022-06-25 18:05:48
...

最近有很多朋友询问 springmvc @RequestBody绑定泛型集合的问题,我把解决方案整理了一下供大家参考。

 

1、

http://ahlearns.wordpress.com/2011/07/29/spring-3-mvc-requestbody-deserialization-of-custom-list-objects/

// The wrapper class, so that type info (of User) is passed thru the message converters to Jackson
public class Users extends ArrayList<User> {
    private static final long serialVersionUID = 1L;
 
    public Users() { super(); }
}
 
// Example method from the controller:
    @RequestMapping(value="/status", method = RequestMethod.POST)
    public @ResponseBody Map<String, ? extends Object> status(@RequestBody Users users) {
        // handle the request here
    }

 

2、使用数组替代

 

3、spring 3.2 直接支持泛型集合

https://jira.springsource.org/browse/SPR-9570 

需要使用Jackson 2.0及以上 如

 <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>

 

 

sprng3.2支持如List<Sample> Map<String, Sample>等集合泛型。