spring rest 接收json数据

 1 @RequestMapping("/appservices/appinfo")
 2 @Controller
 3 public class AppInfoWebService {
 4 
 5     @RequestMapping(value = "/get", method = RequestMethod.POST)
 6     @ResponseBody
 7     public AccountBean show(@RequestBody String appId) {
 8         System.out.println(appId);
 9         
10         ObjectMapper mapper = new ObjectMapper();
11         try {
12             Map<String, Object> account = mapper.readValue(appId, Map.class);
13             for(Map.Entry<String, Object> entry : account.entrySet()) {
14                 Object obj = entry.getValue();
15                 if(obj instanceof String) {
16                     System.out.println(obj.toString());
17                 } else if(obj instanceof List) {
18                     List<Integer> value = (List)obj;
19                     for(Integer sub : value) {
20                         System.out.println("sub:::" + sub);
21                     }
22                 }
23                 System.out.println(entry.getKey() + ":" + entry.getValue());
24             }
25         } catch (Exception e) {
26             // TODO Auto-generated catch block
27             e.printStackTrace();
28         }
29         
30 //        for (Map.Entry<String, Object> entry : appId.entrySet()) {
31 //            System.out.println(entry.getKey() + ":" + entry.getValue());
32 //        }
33         AccountBean bean = new AccountBean();
34         bean.setAddress("china-Guangzhou");
35         bean.setEmail("hoojo_@126.com");
36         bean.setId(1);
37         bean.setName("hoojo");
38         Birthday birthday = new Birthday();
39         birthday.setBirthday("1983.08.01");
40         bean.setBirthday(birthday);
41 
42         return bean;
43     }
44 }


上传示例:

{"address":"bbb","list":[111,222,333]}

 

如果发现415错误,请增加头部

"Content-Type", "application/json; charset=utf-8"

转载于:https://www.cnblogs.com/zenggui/archive/2013/05/16/3081943.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值