httpClient請求

1.  將 pojo 轉換成json ,使用 HttpClient 發送請求java

     1. 1  在 OrderServiceImpl 中json

@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 中cookie

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

2。  將全部參數添加在Map<String,String>中,使用HttpClient發送請求app

       根據參數查詢分頁ide

    

@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 中ui

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中url

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

 

/search/query 接口code

@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);
		
	}

 

總結:發佈的接口 中參數 是Integer類型,使用HttpCliet請求時 能夠 使用 String 類型數據發送orm

相關文章
相關標籤/搜索