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"