Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
yangxiujun
/
paidan_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit ed3c80ca
authored
Jun 14, 2023
by
丁伟峰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
请求应答报文日志打印
1 parent
0f009156
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
99 additions
and
0 deletions
project-order/src/main/java/com/alibaba/cloud/integration/order/WebConfig.java
project-order/src/main/java/com/alibaba/cloud/integration/order/interceptor/RequestInterceptor.java
project-order/src/main/java/com/alibaba/cloud/integration/order/WebConfig.java
0 → 100644
View file @
ed3c80c
package
com
.
alibaba
.
cloud
.
integration
.
order
;
import
com.alibaba.cloud.integration.order.interceptor.RequestInterceptor
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.data.jpa.repository.config.EnableJpaRepositories
;
import
org.springframework.web.servlet.config.annotation.InterceptorRegistry
;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurer
;
@EnableJpaRepositories
@Configuration
public
class
WebConfig
implements
WebMvcConfigurer
{
@Autowired
private
RequestInterceptor
requestInterceptor
;
@Override
public
void
addInterceptors
(
InterceptorRegistry
registry
)
{
registry
.
addInterceptor
(
requestInterceptor
).
addPathPatterns
(
"/**"
);
}
}
project-order/src/main/java/com/alibaba/cloud/integration/order/interceptor/RequestInterceptor.java
0 → 100644
View file @
ed3c80c
package
com
.
alibaba
.
cloud
.
integration
.
order
.
interceptor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.aspectj.lang.JoinPoint
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.*
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
org.springframework.web.servlet.HandlerInterceptor
;
import
javax.servlet.http.HttpServletRequest
;
@Slf4j
@Aspect
@Component
public
class
RequestInterceptor
implements
HandlerInterceptor
{
/**
* 以 controller 包下定义的所有请求为切入点
*/
@Pointcut
(
value
=
"execution(public * com.alibaba.cloud.integration.order.controller..*.*(..))"
)
public
void
reqOpenAPILog
()
{
}
/**
* 在切点之前织入
*
* @param joinPoint
* @throws Throwable
*/
@Before
(
"reqOpenAPILog()"
)
public
void
doBefore
(
JoinPoint
joinPoint
)
throws
Throwable
{
// 开始打印请求日志
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletRequest
request
=
attributes
.
getRequest
();
// 打印请求 url
log
.
info
(
"Request URL : {}"
,
request
.
getRequestURL
().
toString
());
// 打印 Http method
log
.
info
(
"HTTP Method : {}"
,
request
.
getMethod
());
// 打印调用 controller 的全路径以及执行方法
log
.
info
(
"Class Method : {}.{}"
,
joinPoint
.
getSignature
().
getDeclaringTypeName
(),
joinPoint
.
getSignature
().
getName
());
// 打印请求的 IP
log
.
info
(
"Request IP : {}"
,
request
.
getRemoteAddr
());
// 打印请求入参
log
.
info
(
"Request Args : {}"
,
joinPoint
.
getArgs
());
}
/**
* 在切点之后织入
*
* @throws Throwable
*/
@After
(
"reqOpenAPILog()"
)
public
void
doAfter
()
throws
Throwable
{
// TODO
}
/**
* 环绕
*
* @param proceedingJoinPoint
* @return
* @throws Throwable
*/
@Around
(
"reqOpenAPILog()"
)
public
Object
doAround
(
ProceedingJoinPoint
proceedingJoinPoint
)
throws
Throwable
{
log
.
info
(
"========================================== Start =========================================="
);
long
startTime
=
System
.
currentTimeMillis
();
Object
result
=
proceedingJoinPoint
.
proceed
();
// 打印出参
log
.
info
(
"Response Args : {}"
,
result
);
// 执行耗时
log
.
info
(
"Time-Consuming : {} ms"
,
System
.
currentTimeMillis
()
-
startTime
);
log
.
info
(
"=========================================== End ==========================================="
);
return
result
;
}
}
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