Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
architecture
/
glsx-archetype
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit cf4da388
authored
Apr 18, 2023
by
刘宇峰
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
框架完善
1 parent
10acc0e4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
157 additions
and
2 deletions
rest-archetype-framework/rest-admin-feign/pom.xml
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/interceptor/RestLog.java
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/interceptor/RestTemplateRequestInterceptor.java
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/utils/RestTemplateUtils.java
rest-archetype-framework/rest-admin-feign/pom.xml
View file @
cf4da38
...
...
@@ -24,7 +24,6 @@
<groupId>
io.github.openfeign
</groupId>
<artifactId>
feign-okhttp
</artifactId>
</dependency>
<!--json-->
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
...
...
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/interceptor/RestLog.java
0 → 100644
View file @
cf4da38
package
com
.
glsx
.
plat
.
feign
.
interceptor
;
import
lombok.Builder
;
import
lombok.Data
;
import
org.springframework.http.HttpHeaders
;
/**
* 定义日志的字段
*/
@Data
@Builder
public
class
RestLog
{
private
String
reqUrl
;
private
String
method
;
private
HttpHeaders
headers
;
private
String
reqBody
;
private
String
resBody
;
private
long
costTime
;
private
int
resStatus
;
}
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/interceptor/RestTemplateRequestInterceptor.java
0 → 100644
View file @
cf4da38
package
com
.
glsx
.
plat
.
feign
.
interceptor
;
import
cn.hutool.json.JSONUtil
;
import
com.glsx.plat.core.enums.SysConstants
;
import
com.glsx.plat.core.web.Result
;
import
com.glsx.plat.exception.ServiceException
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.HttpRequest
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.client.ClientHttpRequestExecution
;
import
org.springframework.http.client.ClientHttpRequestInterceptor
;
import
org.springframework.http.client.ClientHttpResponse
;
import
org.springframework.util.StopWatch
;
import
org.springframework.util.StreamUtils
;
import
java.io.*
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Objects
;
/**
* 自定义的拦截器
* <p>
* 主要打印日志
*/
@Slf4j
public
class
RestTemplateRequestInterceptor
implements
ClientHttpRequestInterceptor
{
@Override
public
ClientHttpResponse
intercept
(
HttpRequest
httpRequest
,
byte
[]
body
,
ClientHttpRequestExecution
clientHttpRequestExecution
)
throws
IOException
{
//记录请求开始时间
StopWatch
stopWatch
=
new
StopWatch
();
stopWatch
.
start
();
//执行请求
ClientHttpResponse
response
=
clientHttpRequestExecution
.
execute
(
httpRequest
,
body
);
//执行完毕后,这里要创建备份,要不然会报io提前关闭的异常错误
ClientHttpResponse
responseCopy
=
new
BufferingClientHttpResponseWrapper
(
response
);
//记录请求结束时间
stopWatch
.
stop
();
//获取响应内容
StringBuilder
resBody
=
new
StringBuilder
();
try
(
BufferedReader
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
responseCopy
.
getBody
(),
StandardCharsets
.
UTF_8
)))
{
String
line
=
bufferedReader
.
readLine
();
while
(
line
!=
null
)
{
resBody
.
append
(
line
);
line
=
bufferedReader
.
readLine
();
}
}
//获取请求内容
String
reqBody
=
""
;
//这里可以自定义想打印什么方法或者请求的内容,如下只打印post请求的日志,因为这时候才会带上body
if
(
httpRequest
.
getHeaders
().
getContentType
()
!=
null
&&
"POST"
.
equals
(
httpRequest
.
getMethod
().
name
()))
{
String
requestBody
=
new
String
(
body
,
StandardCharsets
.
UTF_8
);
//String contentType = httpRequest.getHeaders().getContentType().toString();
//这里可以对contentType做一些判断,针对其格式化请求体的内容,如"application/x-www-form-urlencoded"格式会附带一些boundary(分割线)的内容
reqBody
=
requestBody
;
}
//打印日志的格式可以自定义
log
.
info
(
JSONUtil
.
toJsonStr
(
RestLog
.
builder
()
.
costTime
(
stopWatch
.
getLastTaskTimeMillis
())
.
headers
(
httpRequest
.
getHeaders
())
.
method
(
httpRequest
.
getMethodValue
())
.
reqUrl
(
httpRequest
.
getURI
().
toString
())
.
reqBody
(
reqBody
)
.
resBody
(
resBody
.
toString
())
.
resStatus
(
responseCopy
.
getRawStatusCode
())
.
build
()));
Result
result
=
JSONUtil
.
toBean
(
reqBody
,
Result
.
class
);
if
(
Objects
.
equals
(
SysConstants
.
ResultStatus
.
success
.
getCode
(),
result
.
getCode
()))
{
throw
ServiceException
.
create
(
String
.
valueOf
(
result
.
getCode
()),
result
.
getMessage
());
}
return
responseCopy
;
}
}
/**
* 响应内容备份
*/
final
class
BufferingClientHttpResponseWrapper
implements
ClientHttpResponse
{
private
final
ClientHttpResponse
response
;
private
byte
[]
body
;
BufferingClientHttpResponseWrapper
(
ClientHttpResponse
response
)
{
this
.
response
=
response
;
}
@Override
public
HttpStatus
getStatusCode
()
throws
IOException
{
return
this
.
response
.
getStatusCode
();
}
@Override
public
int
getRawStatusCode
()
throws
IOException
{
return
this
.
response
.
getRawStatusCode
();
}
@Override
public
String
getStatusText
()
throws
IOException
{
return
this
.
response
.
getStatusText
();
}
@Override
public
HttpHeaders
getHeaders
()
{
return
this
.
response
.
getHeaders
();
}
@Override
public
InputStream
getBody
()
throws
IOException
{
if
(
this
.
body
==
null
)
{
this
.
body
=
StreamUtils
.
copyToByteArray
(
this
.
response
.
getBody
());
}
return
new
ByteArrayInputStream
(
this
.
body
);
}
@Override
public
void
close
()
{
this
.
response
.
close
();
}
}
\ No newline at end of file
rest-archetype-framework/rest-admin-feign/src/main/java/com/glsx/plat/feign/utils/RestTemplateUtils.java
View file @
cf4da38
package
com
.
glsx
.
plat
.
feign
.
utils
;
import
com.glsx.plat.feign.interceptor.RestTemplateRequestInterceptor
;
import
org.springframework.core.ParameterizedTypeReference
;
import
org.springframework.http.*
;
import
org.springframework.http.client.ClientHttpRequestInterceptor
;
import
org.springframework.http.client.OkHttp3ClientHttpRequestFactory
;
import
org.springframework.http.client.SimpleClientHttpRequestFactory
;
import
org.springframework.util.MultiValueMap
;
...
...
@@ -11,6 +13,8 @@ import org.springframework.web.client.RestTemplate;
import
org.springframework.web.util.UriComponentsBuilder
;
import
java.lang.reflect.ParameterizedType
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Objects
;
public
class
RestTemplateUtils
{
...
...
@@ -29,7 +33,14 @@ public class RestTemplateUtils {
OkHttp3ClientHttpRequestFactory
factory
=
new
OkHttp3ClientHttpRequestFactory
();
factory
.
setConnectTimeout
(
CONNECT_TIMEOUT
);
factory
.
setReadTimeout
(
SOCKET_TIMEOUT
);
return
new
RestTemplate
(
factory
);
RestTemplate
restTemplate
=
new
RestTemplate
(
factory
);
//添加拦截器
List
<
ClientHttpRequestInterceptor
>
interceptors
=
new
ArrayList
<>();
RestTemplateRequestInterceptor
requestInterceptor
=
new
RestTemplateRequestInterceptor
();
interceptors
.
add
(
requestInterceptor
);
restTemplate
.
setInterceptors
(
interceptors
);
return
restTemplate
;
}
public
static
<
T
>
T
fetch
(
String
url
,
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment