码迷,mamicode.com
首页 > 编程语言 > 详细

spring restTemplate 用法

时间:2015-08-26 18:06:26      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

1、spring xml 配置

<!-- JSON转换器 -->
	<bean id="jsonConverter"
		class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
				<value>application/json; charset=UTF-8</value>
				
			</list>
		</property>
	</bean>
	<!-- Client -->
	<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
		<property name="messageConverters">
			<list>
				<ref bean="jsonConverter" />
			</list>
		</property>
	</bean>



2、code

@Autowired
	RestTemplate restTemplate;
	public static  final String syncChatUrl = "/rest/mucroom/sync?type={type}&doorPass={doorPass}";

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<String, Object> syncChat(int type) {
		try {
			Map<String, Object> resultMap = new HashMap<String, Object>();
			HashMap<String, String> uriVariables = new HashMap<String, String>();
			uriVariables.put("type", type + "");
			uriVariables.put("doorPass", "dafjdasklfjasklfjkasl");
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_JSON);
			HttpEntity<String> requestEntity = new HttpEntity<String>(headers);

			ResponseEntity<Map> body = restTemplate.exchange(iflahbuChatUrl + TimerConstant.syncChatUrl, HttpMethod.GET, requestEntity, Map.class,uriVariables);
					resultMap =	body.getBody();
			return resultMap;
		} catch (Exception e) {
			logger.error("sync chat error:" + e);
		}



spring restTemplate 用法

标签:

原文地址:http://my.oschina.net/fengshuzi/blog/497380

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!