Spring MVC 3学习笔记+教程(三)返回JSON数据

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.2cto.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}]
 
的数据。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值