Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | About

4.3. postForObject

4.3.1. 传递对象

			
	@RequestMapping("/restful/post/{id}")
	@ResponseBody
	private static String restfullPost(@PathVariable String id) {

		final String uri = "http://inf.netkiller.cn/detail/html/{tid}/{cid}/{id}.html";

		Map<String, String> params = new HashMap<String, String>();
		params.put("tid", "2");
		params.put("cid", "2");
		params.put("id", id);

		City city = new City("Shenzhen", "Guangdong");

		RestTemplate restTemplate = new RestTemplate();
		String result = restTemplate.postForObject(uri, city, String.class, params);
		return result;
	}			
			
			

4.3.2. 传递数据结构 MultiValueMap

			
	@RequestMapping("/findByMobile")
	public String findByMobile() {
		System.out.println("****************************findByMobile******************************");

		final String uri = "http://www.netkiller.cn/account/getMemberByMobile.json";
		MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
		try {

			map.add("prefix", "86");
			map.add("mobile", "13698041116");
			map.add("_pretty_", "false");

		} catch (Exception e) {
			e.printStackTrace();
		}

		RestTemplate restTemplate = new RestTemplate();
		String result = restTemplate.postForObject(uri, map, String.class);

		System.out.println(map.toString());
		System.out.println(result);

		return result;
	}