Commit a23e0b09 authored by 耿迪迪's avatar 耿迪迪

httpClient工具类添加

parent 2c51e3c6
...@@ -58,13 +58,13 @@ spring: ...@@ -58,13 +58,13 @@ spring:
# redis 配置 # redis 配置
redis: redis:
# 地址 # 地址
host: 127.0.01 host: 36.138.180.82
# 端口,默认为6379 # 端口,默认为6379
port: 6379 port: 63798
# 数据库索引 # 数据库索引
database: 0 database: 0
# 密码 # 密码
password: password: 1qaz2wsx3edc@
# 连接超时时间 # 连接超时时间
timeout: 10s timeout: 10s
lettuce: lettuce:
...@@ -97,4 +97,4 @@ zehong: ...@@ -97,4 +97,4 @@ zehong:
captchaType: math captchaType: math
shelf: shelf:
apiUrl: http://192.168.3117:8099 apiUrl: http://192.168.3.117:8099
\ No newline at end of file \ No newline at end of file
...@@ -125,6 +125,13 @@ ...@@ -125,6 +125,13 @@
<artifactId>spring-boot-starter-websocket</artifactId> <artifactId>spring-boot-starter-websocket</artifactId>
</dependency> </dependency>
<!--httpclient-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
package com.zehong.common.utils.http;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtils {
private static final Logger log = LoggerFactory.getLogger(HttpClientUtils.class);
/**
* get请求
* @param url 请求地址
* @param param 请求参数
* @headData headData 请求头
* @return String
*/
public static String doGet(String url, Map<String, Object> param, Map<String, Object> headData) {
//创建默认的httpclient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
//响应对象
CloseableHttpResponse response = null;
//返回结果
String resultStr = "";
try {
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, String.valueOf(param.get(key)));
}
}
URI uri = builder.build();
HttpGet httpGet = new HttpGet(uri);
//携带head数据
if (headData != null && headData.size() > 0) {
for (String key : headData.keySet()) {
httpGet.setHeader(key, (String) headData.get(key));
}
}
response = httpClient.execute(httpGet);
if (response != null && response.getStatusLine().getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
resultStr = EntityUtils.toString(httpEntity, "UTF-8");
log.info("recv - {}", resultStr);
}
}catch (Exception e){
log.error("HttpClientUtils.doGet Exception, url=" + url + ",param=" + param, e);
}finally {
close(response,httpClient);
}
return resultStr;
}
/**
* post请求
* @param url 请求地址
* @param param 请求参数
* @param headData 请求头
* @return String
* @throws IOException 异常信息
*/
public static String doPost(String url, Map<String, Object> param, Map<String, Object> headData) {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
HttpPost httpPost = new HttpPost(url);
if (param != null) {
List<NameValuePair> formParams = new ArrayList<>();
for (String key : param.keySet()) {
formParams.add(new BasicNameValuePair(key, String.valueOf(param.get(key))));
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams,"UTF-8");
httpPost.setEntity(entity);
}
//携带head数据
if (headData != null && headData.size() > 0) {
for (String key : headData.keySet()) {
httpPost.setHeader(key, (String) headData.get(key));
}
}
//执行post请求
response = httpClient.execute(httpPost);
if (response!=null && response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(),"UTF-8");
log.info("recv - {}", resultString);
}
}catch (Exception e){
log.error("HttpClientUtils.doPost Exception, url=" + url + ",param=" + param, e);
}finally {
close(response,httpClient);
}
return resultString;
}
/**
* body传参
* @param url 请求地址
* @param param body参数
* @param headData 请求头
* @return String
* @throws IOException 异常
*/
public static String doPost(String url,String param,Map<String, Object> headData){
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String resultString = "";
try {
HttpPost httpPost = new HttpPost(url);
if (param != null) {
httpPost.setEntity(new StringEntity(param, ContentType.create("application/json", "utf-8")));
}
//携带head数据
if (headData != null && headData.size() > 0) {
for (String key : headData.keySet()) {
httpPost.setHeader(key, (String) headData.get(key));
}
}
//执行post请求
response = httpClient.execute(httpPost);
if (response!=null && response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(),"UTF-8");
log.info("recv - {}", resultString);
}
}catch (Exception e){
log.error("HttpClientUtils.doPost Exception, url=" + url + ",param=" + param, e);
}finally {
close(response,httpClient);
}
return resultString;
}
/**
* 关闭客户端
* @param response 返回实体
* @param httpClient 客户端
* @throws IOException 异常信息
*/
private static void close( CloseableHttpResponse response,CloseableHttpClient httpClient) {
try {
if (response != null) {
response.close();
}
httpClient.close();
} catch (IOException e) {
log.error("HttpClientUtils.close Exception", e);
}
}
public static String uploadFile (String url,HttpEntity entity) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
String result = "";
try {
// 路径自定义
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
// 执行提交
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
} catch (Exception e) {
log.error("HttpClientUtils.uploadFile Exception, url=" + url, e);
} finally {
close(response,httpClient);
}
return result;
}
}
package com.zehong.system.service.impl.shelf; package com.zehong.system.service.impl.shelf;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.zehong.system.service.impl.shelf.annotation.DockingTaskType;
import com.zehong.system.service.impl.shelf.annotation.PlatformDockingTaskLog;
import com.zehong.system.service.impl.shelf.annotation.SendNotification;
import com.zehong.common.exception.CustomException; import com.zehong.common.exception.CustomException;
import com.zehong.common.utils.StringUtils; import com.zehong.common.utils.StringUtils;
import com.zehong.common.utils.http.HttpUtils; import com.zehong.common.utils.http.HttpClientUtils;
import com.zehong.system.domain.shelf.TPlatformDockingTask;
import com.zehong.system.domain.shelf.TShelfInfo; import com.zehong.system.domain.shelf.TShelfInfo;
import com.zehong.system.domain.shelf.TShelfStorageLocation; import com.zehong.system.domain.shelf.TShelfStorageLocation;
import com.zehong.system.mapper.shelf.TPlatformDockingTaskMapper;
import com.zehong.system.mapper.shelf.TShelfInfoMapper; import com.zehong.system.mapper.shelf.TShelfInfoMapper;
import com.zehong.system.mapper.shelf.TShelfStorageLocationMapper; import com.zehong.system.mapper.shelf.TShelfStorageLocationMapper;
import com.zehong.system.service.impl.shelf.annotation.DockingTaskType;
import com.zehong.system.service.impl.shelf.annotation.PlatformDockingTaskLog;
import com.zehong.system.service.impl.shelf.annotation.SendNotification;
import com.zehong.system.service.shelf.ITShelfApiService; import com.zehong.system.service.shelf.ITShelfApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -28,6 +28,8 @@ import java.text.SimpleDateFormat; ...@@ -28,6 +28,8 @@ import java.text.SimpleDateFormat;
@Service @Service
public class ITShelfApiServiceImpl implements ITShelfApiService{ public class ITShelfApiServiceImpl implements ITShelfApiService{
private static final Logger log = LoggerFactory.getLogger(ITShelfApiServiceImpl.class);
@Resource @Resource
private TShelfStorageLocationMapper shelfStorageLocationMapper; private TShelfStorageLocationMapper shelfStorageLocationMapper;
...@@ -58,7 +60,8 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{ ...@@ -58,7 +60,8 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{
*/ */
@Override @Override
public JSONObject getRackStatus(String param) { public JSONObject getRackStatus(String param) {
String result = HttpUtils.sendPost(shelfApiUrl + RACK_STATUS_URL,param); log.info("getRackStatus...params==========" + param);
String result = HttpClientUtils.doPost(shelfApiUrl + RACK_STATUS_URL,param,null);
if(StringUtils.isEmpty(result)) throw new CustomException("获取料架状态信息失败"); if(StringUtils.isEmpty(result)) throw new CustomException("获取料架状态信息失败");
return JSONObject.parseObject(result); return JSONObject.parseObject(result);
} }
...@@ -71,7 +74,8 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{ ...@@ -71,7 +74,8 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{
@Override @Override
@PlatformDockingTaskLog(type = DockingTaskType.CALL_RACK_SYSTEM) @PlatformDockingTaskLog(type = DockingTaskType.CALL_RACK_SYSTEM)
public String rackInOutput(String param){ public String rackInOutput(String param){
String result = HttpUtils.sendPost(shelfApiUrl + RACK_IN_OUT_PUT_URL,param); log.info("rackInOutput...param==========" + param);
String result = HttpClientUtils.doPost(shelfApiUrl + RACK_IN_OUT_PUT_URL,param,null);
if(StringUtils.isEmpty(result)) throw new CustomException("料架出入库接口错误"); if(StringUtils.isEmpty(result)) throw new CustomException("料架出入库接口错误");
return result; return result;
} }
...@@ -82,7 +86,8 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{ ...@@ -82,7 +86,8 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{
*/ */
@Override @Override
public JSONObject lookAllLocation(String param){ public JSONObject lookAllLocation(String param){
String result = HttpUtils.sendPost(shelfApiUrl + LOOK_ALL_LOCATION_URL,param); log.info("lookAllLocation...param==========" + param);
String result = HttpClientUtils.doPost(shelfApiUrl + LOOK_ALL_LOCATION_URL,param,null);
if(StringUtils.isEmpty(result)) throw new CustomException("获取料架所有储位信息错误"); if(StringUtils.isEmpty(result)) throw new CustomException("获取料架所有储位信息错误");
return JSONObject.parseObject(result); return JSONObject.parseObject(result);
} }
...@@ -97,6 +102,7 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{ ...@@ -97,6 +102,7 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{
@PlatformDockingTaskLog(type = DockingTaskType.RECEIVE_RACK_SYSTEM_INFO) @PlatformDockingTaskLog(type = DockingTaskType.RECEIVE_RACK_SYSTEM_INFO)
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public JSONObject receiveFeedBackInfo(JSONObject request) { public JSONObject receiveFeedBackInfo(JSONObject request) {
log.info("receiveFeedBackInfo...param==========" + request.toJSONString());
try { try {
JSONObject result = new JSONObject(); JSONObject result = new JSONObject();
TShelfInfo shelfInfo = shelfInfoMapper.selectTShelfInfoByShelf(request.getString("SHELF")); TShelfInfo shelfInfo = shelfInfoMapper.selectTShelfInfoByShelf(request.getString("SHELF"));
...@@ -118,7 +124,7 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{ ...@@ -118,7 +124,7 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{
result.put("ERRORCODE",""); result.put("ERRORCODE","");
return result; return result;
} catch (ParseException e) { } catch (ParseException e) {
throw new CustomException("储出储成功后反馈接口错误"); throw new CustomException("储出储成功后反馈接口错误",e);
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment