Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
smart-rack-base
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
耿迪迪
smart-rack-base
Commits
a23e0b09
Commit
a23e0b09
authored
Oct 14, 2025
by
耿迪迪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
httpClient工具类添加
parent
2c51e3c6
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
226 additions
and
14 deletions
+226
-14
application-test.yml
...t-rack-base-admin/src/main/resources/application-test.yml
+4
-4
pom.xml
smart-rack-base-common/pom.xml
+7
-0
HttpClientUtils.java
...in/java/com/zehong/common/utils/http/HttpClientUtils.java
+199
-0
ITShelfApiServiceImpl.java
...hong/system/service/impl/shelf/ITShelfApiServiceImpl.java
+16
-10
No files found.
smart-rack-base-admin/src/main/resources/application-test.yml
View file @
a23e0b09
...
...
@@ -58,13 +58,13 @@ spring:
# redis 配置
redis
:
# 地址
host
:
127.0.01
host
:
36.138.180.82
# 端口,默认为6379
port
:
6379
port
:
6379
8
# 数据库索引
database
:
0
# 密码
password
:
password
:
1qaz2wsx3edc@
# 连接超时时间
timeout
:
10s
lettuce
:
...
...
@@ -97,4 +97,4 @@ zehong:
captchaType
:
math
shelf
:
apiUrl
:
http://192.168.3117:8099
\ No newline at end of file
apiUrl
:
http://192.168.3.117:8099
\ No newline at end of file
smart-rack-base-common/pom.xml
View file @
a23e0b09
...
...
@@ -125,6 +125,13 @@
<artifactId>
spring-boot-starter-websocket
</artifactId>
</dependency>
<!--httpclient-->
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<version>
4.5.2
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
smart-rack-base-common/src/main/java/com/zehong/common/utils/http/HttpClientUtils.java
0 → 100644
View file @
a23e0b09
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
;
}
}
smart-rack-base-system/src/main/java/com/zehong/system/service/impl/shelf/ITShelfApiServiceImpl.java
View file @
a23e0b09
package
com
.
zehong
.
system
.
service
.
impl
.
shelf
;
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.utils.StringUtils
;
import
com.zehong.common.utils.http.HttpUtils
;
import
com.zehong.system.domain.shelf.TPlatformDockingTask
;
import
com.zehong.common.utils.http.HttpClientUtils
;
import
com.zehong.system.domain.shelf.TShelfInfo
;
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.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
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
...
...
@@ -28,6 +28,8 @@ import java.text.SimpleDateFormat;
@Service
public
class
ITShelfApiServiceImpl
implements
ITShelfApiService
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
ITShelfApiServiceImpl
.
class
);
@Resource
private
TShelfStorageLocationMapper
shelfStorageLocationMapper
;
...
...
@@ -58,7 +60,8 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{
*/
@Override
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
(
"获取料架状态信息失败"
);
return
JSONObject
.
parseObject
(
result
);
}
...
...
@@ -71,7 +74,8 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{
@Override
@PlatformDockingTaskLog
(
type
=
DockingTaskType
.
CALL_RACK_SYSTEM
)
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
(
"料架出入库接口错误"
);
return
result
;
}
...
...
@@ -82,7 +86,8 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{
*/
@Override
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
(
"获取料架所有储位信息错误"
);
return
JSONObject
.
parseObject
(
result
);
}
...
...
@@ -97,6 +102,7 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{
@PlatformDockingTaskLog
(
type
=
DockingTaskType
.
RECEIVE_RACK_SYSTEM_INFO
)
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
JSONObject
receiveFeedBackInfo
(
JSONObject
request
)
{
log
.
info
(
"receiveFeedBackInfo...param=========="
+
request
.
toJSONString
());
try
{
JSONObject
result
=
new
JSONObject
();
TShelfInfo
shelfInfo
=
shelfInfoMapper
.
selectTShelfInfoByShelf
(
request
.
getString
(
"SHELF"
));
...
...
@@ -118,7 +124,7 @@ public class ITShelfApiServiceImpl implements ITShelfApiService{
result
.
put
(
"ERRORCODE"
,
""
);
return
result
;
}
catch
(
ParseException
e
)
{
throw
new
CustomException
(
"储出储成功后反馈接口错误"
);
throw
new
CustomException
(
"储出储成功后反馈接口错误"
,
e
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment