diff --git a/src/main/java/com/casic/controller/DataForwardingController.java b/src/main/java/com/casic/controller/DataForwardingController.java index 79080e3..9e1ea10 100644 --- a/src/main/java/com/casic/controller/DataForwardingController.java +++ b/src/main/java/com/casic/controller/DataForwardingController.java @@ -2,14 +2,12 @@ import com.alibaba.fastjson.JSONObject; import com.casic.util.HttpClientUtil; -import com.casic.util.ServerSocketUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import javax.annotation.Resource; import java.util.Map; @Slf4j @@ -18,21 +16,21 @@ @Value("${casic.url}") private String url; - @Resource - private ServerSocketUtil serverSocketUtil; @RequestMapping("/data/forwarding") public String dataForwarding(@RequestBody Map map) { JSONObject retObj = new JSONObject(); log.info(JSONObject.toJSONString(map)); + String resp = ""; try { - HttpClientUtil.doPostJson("##" + JSONObject.toJSONString(map) + "**", url); -// serverSocketUtil.sendMsg(JSONObject.toJSONString(map)); + resp = HttpClientUtil.doPostJson("##" + JSONObject.toJSONString(map) + "**", url); } catch (Exception ex) { - log.error("错误"); + log.error("错误: " + ex.getMessage()); } + retObj.put("response", resp); retObj.put("code", 200); retObj.put("success", true); + log.info(retObj.toJSONString()); return retObj.toJSONString(); } } diff --git a/src/main/java/com/casic/controller/DataForwardingController.java b/src/main/java/com/casic/controller/DataForwardingController.java index 79080e3..9e1ea10 100644 --- a/src/main/java/com/casic/controller/DataForwardingController.java +++ b/src/main/java/com/casic/controller/DataForwardingController.java @@ -2,14 +2,12 @@ import com.alibaba.fastjson.JSONObject; import com.casic.util.HttpClientUtil; -import com.casic.util.ServerSocketUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import javax.annotation.Resource; import java.util.Map; @Slf4j @@ -18,21 +16,21 @@ @Value("${casic.url}") private String url; - @Resource - private ServerSocketUtil serverSocketUtil; @RequestMapping("/data/forwarding") public String dataForwarding(@RequestBody Map map) { JSONObject retObj = new JSONObject(); log.info(JSONObject.toJSONString(map)); + String resp = ""; try { - HttpClientUtil.doPostJson("##" + JSONObject.toJSONString(map) + "**", url); -// serverSocketUtil.sendMsg(JSONObject.toJSONString(map)); + resp = HttpClientUtil.doPostJson("##" + JSONObject.toJSONString(map) + "**", url); } catch (Exception ex) { - log.error("错误"); + log.error("错误: " + ex.getMessage()); } + retObj.put("response", resp); retObj.put("code", 200); retObj.put("success", true); + log.info(retObj.toJSONString()); return retObj.toJSONString(); } } diff --git a/src/main/java/com/casic/util/HttpClientUtil.java b/src/main/java/com/casic/util/HttpClientUtil.java index 16101b3..467cf4a 100644 --- a/src/main/java/com/casic/util/HttpClientUtil.java +++ b/src/main/java/com/casic/util/HttpClientUtil.java @@ -7,6 +7,7 @@ import java.util.Map; import org.apache.http.NameValuePair; +import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; @@ -104,29 +105,34 @@ return doPost(url, null); } - public static String doPostJson(String json, String url) { + public static String doPostJson(String json, String url) throws Exception { // 创建Httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; + RequestConfig requestConfig = RequestConfig.custom() + .setConnectionRequestTimeout(3000) + .setSocketTimeout(3000) + .setConnectTimeout(3000) + .build(); String resultString = ""; try { // 创建Http Post请求 HttpPost httpPost = new HttpPost(url); + httpPost.setConfig(requestConfig); // 创建请求内容 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(); + } catch (Exception ex) { + throw ex; } finally { - try { + if (null != response) { response.close(); + } + if (null != httpClient) { httpClient.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); } }