RestTemplate解决返回类型为Object问题

代码如下:

	private static final String DEFAULT_MEDIA_TYPE = "application/json; charset=UTF-8";
	private static final String QUERY_USER_AUTHORIZED_MENU_TREE = "queryMenuTree";
	
	@Override
	public List getMenuTreeByUserId(String userId) {
		
		String targetUrl = getTargetUrl(systemMenuAddress, QUERY_USER_AUTHORIZED_MENU_TREE);
		
		//如果没有数据 返回空集合
		List treeNodeList = new ArrayList();
		RestTemplate restTemplate = SimpleRestClient.getClient();
		
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType(DEFAULT_MEDIA_TYPE);
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		HttpEntity entity = new HttpEntity(headers);

		ResponseEntity>> response = restTemplate.exchange(targetUrl + "?userId=" + userId, 
                HttpMethod.GET, 
                entity, 
                new ParameterizedTypeReference>>(){});
		
		if(response != null ){
			
			Result> resultObj = response.getBody();
			return  (List) resultObj.getData();
			
		}
		return treeNodeList;
	}

 

 

转载于:https://my.oschina.net/u/2246525/blog/1583457

你可能感兴趣的:(RestTemplate解决返回类型为Object问题)