您的位置:首页 > 编程语言 > Java开发

spring rest 接收json数据

2013-05-16 15:28 288 查看
@RequestMapping("/appservices/appinfo")
@Controller
public class AppInfoWebService {

@RequestMapping(value = "/get", method = RequestMethod.POST)
@ResponseBody
public AccountBean show(@RequestBody String appId) {
System.out.println(appId);

ObjectMapper mapper = new ObjectMapper();
try {
Map<String, Object> account = mapper.readValue(appId, Map.class);
for(Map.Entry<String, Object> entry : account.entrySet()) {
Object obj = entry.getValue();
if(obj instanceof String) {
System.out.println(obj.toString());
} else if(obj instanceof List) {
List<Integer> value = (List)obj;
for(Integer sub : value) {
System.out.println("sub:::" + sub);
}
}
System.out.println(entry.getKey() + ":" + entry.getValue());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//        for (Map.Entry<String, Object> entry : appId.entrySet()) {
//            System.out.println(entry.getKey() + ":" + entry.getValue());
//        }
AccountBean bean = new AccountBean();
bean.setAddress("china-Guangzhou");
bean.setEmail("hoojo_@126.com");
bean.setId(1);
bean.setName("hoojo");
Birthday birthday = new Birthday();
birthday.setBirthday("1983.08.01");
bean.setBirthday(birthday);

return bean;
}
}


上传示例:

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

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

"Content-Type", "application/json; charset=utf-8"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: