Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | 雲棲社區 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Github | Search | About

7.3. postForObject

7.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;
	}			
			
			

7.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;
	}