125°

httpClient请求

1.  将 pojo 转换成json ,使用 HttpClient 发送请求

     1. 1  在 OrderServiceImpl 中

@Override
	public String createOrder(Order order) {
		//调用创建订单服务之前补全用户信息。
		//从cookie中后取TT_TOKEN的内容,根据token调用sso系统的服务根据token换取用户信息。
	//调用taotao-order的服务提交订单。
	String json = HttpClientUtil.doPostJson(ORDER_BASE_URL + ORDER_CREATE_URL, JsonUtils.objectToJson(order));
	//把json转换成taotaoResult
	TaotaoResult taotaoResult = TaotaoResult.format(json);
	if (taotaoResult.getStatus() == 200) {
		Object orderId = taotaoResult.getData();
		return orderId.toString();
	}
	return "";
}

1.2 在 HttpClientUtil 中

public static String doPostJson(String url, String json) {
		// 创建Httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String resultString = "";
		try {
			// 创建Http Post请求
			HttpPost httpPost = new HttpPost(url);
			// 创建请求内容
			StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
			httpPost.setEntity(entity);
			// 执行http请求
			response = httpClient.execute(httpPost);
			resultString = EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				response.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	return resultString;
}</code></pre> 

2。  将所有参数添加在Map<String,String>中,使用HttpClient发送请求

       根据参数查询分页

    

@Override
	public SearchResult search(String queryString, int page) {
		// 调用taotao-search的服务
		//查询参数
		Map<String, String> param = new HashMap<>();
		param.put("q", queryString);
		param.put("page", page + "");
		try {
			//调用服务
			String json = HttpClientUtil.doGet(SEARCH_BASE_URL, param);
			//把字符串转换成java对象
			TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, SearchResult.class);
			if (taotaoResult.getStatus() == 200) {
				SearchResult result = (SearchResult) taotaoResult.getData();
				return result;
			}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
}

HttpClientUtil 中

public static String doGet(String url, Map<String, String> param) {
	// 创建Httpclient对象
	CloseableHttpClient httpclient = HttpClients.createDefault();

	String resultString = "";
	CloseableHttpResponse response = null;
	try {
		// 创建uri
		URIBuilder builder = new URIBuilder(url);
		if (param != null) {
			for (String key : param.keySet()) {
				builder.addParameter(key, param.get(key));
			}
		}
		URI uri = builder.build();

		// 创建http GET请求
		HttpGet httpGet = new HttpGet(uri);

		// 执行请求
		response = httpclient.execute(httpGet);
		// 判断返回状态是否为200
		if (response.getStatusLine().getStatusCode() == 200) {
			resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if (response != null) {
				response.close();
			}
			httpclient.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return resultString;
}

 

其中 SEARCH_BASE_URL 配置在 resource.properties中

SEARCH_BASE_URL=http://192.168.25.136:8081/search/query

 

/search/query 接口

@RequestMapping(value="/query", method=RequestMethod.GET)
	@ResponseBody
	public TaotaoResult search(@RequestParam("q")String queryString, 
			@RequestParam(defaultValue="1")Integer page, 
			@RequestParam(defaultValue="60")Integer rows) {
		//查询条件不能为空
		if (StringUtils.isBlank(queryString)) {
			return TaotaoResult.build(400, "查询条件不能为空");
		}
		SearchResult searchResult = null;
		try {
			queryString = new String(queryString.getBytes("iso8859-1"), "utf-8");
			searchResult = searchService.search(queryString, page, rows);
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
		return TaotaoResult.ok(searchResult);
}</code></pre> 

 

总结:发布的接口 中参数 是Integer类型,使用HttpCliet请求时 可以 使用 String 类型数据发送

本文由【杨】发布于开源中国,原文链接:https://my.oschina.net/u/3631797/blog/3157904

全部评论: 0

    我有话说: