Spring MVC 3学习笔记+教程(三)返回JSON数据
4247 点击·0 回帖
![]() | ![]() | |
![]() | Spring MVC响应中返回JSON数据的方法: 配置与以前相同使用<mvc:annotation-driven/>的注解配置, 但web-INF/lib的类路径里面要有jackson-all-1.6.9.jar这个库文件, 然后在controller里面这样写: @Controller @RequestMapping("/user" ) public class UserController { @RequestMapping("/ajax2" ) @ResponseBody public List<User> ajax2(){ System. out.println("ajax request2--" ); List<User> userList= new ArrayList<User>(); for(int i=0; i<5; i++){ User user= new User(); user.setName( "测试"+(i+1)); user.setPassword( "1234"+i); user. setBirthday(new Date()); userList.add(user); } return userList; } www.atcpu.com 加粗的表示这是返回由jackson转换后的json对象,可以是对象、数组、列表等,在地址里 敲入:http://localhost:8080/spring/user/ajax2 (这里工程context路径是spring), 会返回 [{birthday:1339237340265, name:测试1, password:12340} , {birthday:1339237340265, name:测试2, password:12341} , {birthday:1339237340265, name:测试3, password:12342} , {birthday:1339237340265, name:测试4, password:12343} , {birthday:1339237340265, name:测试5, password:12344}] 的数据。 yanqlv的专栏 | |
![]() | ![]() |