Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
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
YZG
yzg-util
Commits
b1cff2dd
Commit
b1cff2dd
authored
Mar 22, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
整理框架
parent
acede980
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
75 additions
and
23 deletions
+75
-23
ExceptionHelper.java
.../java/com/yanzuoguang/util/exception/ExceptionHelper.java
+1
-3
ResponseResult.java
...src/main/java/com/yanzuoguang/util/vo/ResponseResult.java
+52
-2
FeignAspect.java
.../src/main/java/com/yanzuoguang/cloud/aop/FeignAspect.java
+14
-13
WebAspect.java
...ud/src/main/java/com/yanzuoguang/cloud/aop/WebAspect.java
+8
-5
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/exception/ExceptionHelper.java
View file @
b1cff2dd
...
@@ -3,8 +3,6 @@ package com.yanzuoguang.util.exception;
...
@@ -3,8 +3,6 @@ package com.yanzuoguang.util.exception;
import
com.yanzuoguang.util.contants.ResultConstants
;
import
com.yanzuoguang.util.contants.ResultConstants
;
import
com.yanzuoguang.util.vo.ResponseResult
;
import
com.yanzuoguang.util.vo.ResponseResult
;
import
java.util.logging.Logger
;
/**
/**
* 异常处理帮助类
* 异常处理帮助类
*/
*/
...
@@ -57,7 +55,7 @@ public class ExceptionHelper {
...
@@ -57,7 +55,7 @@ public class ExceptionHelper {
public
static
ResponseResult
getError
(
Exception
e
)
{
public
static
ResponseResult
getError
(
Exception
e
)
{
if
(
e
instanceof
CodeException
)
{
if
(
e
instanceof
CodeException
)
{
CodeException
code
=
(
CodeException
)
e
;
CodeException
code
=
(
CodeException
)
e
;
return
new
ResponseResult
(
code
.
getCode
(),
code
.
getMessage
());
return
new
ResponseResult
(
code
.
getCode
(),
code
.
getMessage
()
,
null
,
code
.
getTarget
()
);
}
else
{
}
else
{
return
new
ResponseResult
(
ResultConstants
.
UNKNOW_ERROR
,
e
.
getMessage
());
return
new
ResponseResult
(
ResultConstants
.
UNKNOW_ERROR
,
e
.
getMessage
());
}
}
...
...
yzg-util-base/src/main/java/com/yanzuoguang/util/vo/ResponseResult.java
View file @
b1cff2dd
...
@@ -24,6 +24,11 @@ public class ResponseResult<T> extends BaseVo {
...
@@ -24,6 +24,11 @@ public class ResponseResult<T> extends BaseVo {
*/
*/
private
T
data
;
private
T
data
;
/**
* 数据源,用于显示异常数据
*/
private
Object
target
;
/**
/**
* 获取结果状态码
* 获取结果状态码
*
*
...
@@ -78,12 +83,32 @@ public class ResponseResult<T> extends BaseVo {
...
@@ -78,12 +83,32 @@ public class ResponseResult<T> extends BaseVo {
this
.
data
=
data
;
this
.
data
=
data
;
}
}
/**
* 获取数据源
*
* @return 数据源
*/
public
Object
getTarget
()
{
return
target
;
}
/**
* 设置数据源
*
* @param target
*/
public
void
setTarget
(
Object
target
)
{
this
.
target
=
target
;
}
/**
/**
* 构造函数
* 构造函数
*/
*/
public
ResponseResult
()
{
public
ResponseResult
()
{
this
(
ResultConstants
.
SUCCESS
,
"操作成功"
);
}
}
/**
/**
* 构造函数
* 构造函数
*
*
...
@@ -91,8 +116,7 @@ public class ResponseResult<T> extends BaseVo {
...
@@ -91,8 +116,7 @@ public class ResponseResult<T> extends BaseVo {
* @param message 结果消息
* @param message 结果消息
*/
*/
public
ResponseResult
(
String
code
,
String
message
)
{
public
ResponseResult
(
String
code
,
String
message
)
{
this
.
code
=
code
;
this
(
code
,
message
,
null
);
this
.
message
=
message
;
}
}
/**
/**
...
@@ -103,9 +127,21 @@ public class ResponseResult<T> extends BaseVo {
...
@@ -103,9 +127,21 @@ public class ResponseResult<T> extends BaseVo {
* @param data 结果
* @param data 结果
*/
*/
public
ResponseResult
(
String
code
,
String
message
,
T
data
)
{
public
ResponseResult
(
String
code
,
String
message
,
T
data
)
{
this
(
code
,
message
,
data
,
null
);
}
/**
* 构造函数
*
* @param code 结果状态
* @param message 结果熊希
* @param data 结果
*/
public
ResponseResult
(
String
code
,
String
message
,
T
data
,
Object
target
)
{
this
.
code
=
code
;
this
.
code
=
code
;
this
.
message
=
message
;
this
.
message
=
message
;
this
.
data
=
data
;
this
.
data
=
data
;
this
.
target
=
target
;
}
}
/**
/**
...
@@ -122,4 +158,18 @@ public class ResponseResult<T> extends BaseVo {
...
@@ -122,4 +158,18 @@ public class ResponseResult<T> extends BaseVo {
ret
.
setData
(
data
);
ret
.
setData
(
data
);
return
ret
;
return
ret
;
}
}
/**
* 处理错误
*
* @param code
* @param message
* @param target
* @param <T>
* @return
*/
public
static
final
<
T
extends
Object
>
ResponseResult
<
T
>
error
(
String
code
,
String
message
,
T
data
,
Object
target
)
{
ResponseResult
<
T
>
ret
=
new
ResponseResult
<>(
code
,
message
,
data
,
target
);
return
ret
;
}
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/FeignAspect.java
View file @
b1cff2dd
...
@@ -40,30 +40,31 @@ public class FeignAspect extends AbstractValidateAspect {
...
@@ -40,30 +40,31 @@ public class FeignAspect extends AbstractValidateAspect {
Object
result
=
null
;
Object
result
=
null
;
long
start
=
System
.
currentTimeMillis
();
long
start
=
System
.
currentTimeMillis
();
try
{
try
{
logger
.
info
(
"
=================[ {} ] feign params is {} =================
"
,
name
,
joinPoint
.
getArgs
());
logger
.
info
(
"
[ {} ] feign params is {}
"
,
name
,
joinPoint
.
getArgs
());
result
=
joinPoint
.
proceed
();
result
=
joinPoint
.
proceed
();
// 假如是标准格式,则验证接口是否成功,不成功则抛出异常
// 假如是标准格式,则验证接口是否成功,不成功则抛出异常
if
(
result
instanceof
ResponseResult
)
{
if
(
result
instanceof
ResponseResult
)
{
ResponseResult
responseResult
=
(
ResponseResult
)
result
;
ResponseResult
responseResult
=
(
ResponseResult
)
result
;
if
(!
ResultConstants
.
SUCCESS
.
equals
(
responseResult
.
getCode
()))
{
if
(!
ResultConstants
.
SUCCESS
.
equals
(
responseResult
.
getCode
()))
{
throw
new
CodeException
(
responseResult
.
getCode
(),
responseResult
.
getMessage
());
throw
new
CodeException
(
responseResult
.
getCode
(),
responseResult
.
getMessage
()
,
responseResult
.
getTarget
()
);
}
}
}
}
// 记录服务调用时间,请求日志
long
end
=
System
.
currentTimeMillis
();
logger
.
info
(
"[ {} ] feign time ({})ms, result is {}"
,
name
,
(
end
-
start
),
result
);
return
result
;
return
result
;
}
catch
(
CodeException
ex
)
{
logger
.
info
(
"=================[ {} ] feign is error {} ================="
,
name
,
ex
);
result
=
new
ResponseResult
(
ex
.
getCode
(),
ex
.
getMessage
());
throw
ex
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
ResponseResult
responseResult
=
ExceptionHelper
.
getError
(
e
);
logger
.
error
(
"=================[ "
+
name
+
" ] feign is error {} ================="
,
e
);
result
=
responseResult
;
throw
new
CodeException
(
responseResult
.
getCode
(),
responseResult
.
getMessage
());
}
finally
{
// 记录服务调用时间,请求日志
long
end
=
System
.
currentTimeMillis
();
long
end
=
System
.
currentTimeMillis
();
logger
.
info
(
"=================[ {} ] feign time ({})ms, result is {} ================="
,
name
,
(
end
-
start
),
result
);
logger
.
error
(
"[ {} ] feign time ({})ms , error {}"
,
name
,
(
end
-
start
),
e
);
if
(
e
instanceof
CodeException
)
{
throw
e
;
}
else
{
ResponseResult
responseResult
=
ExceptionHelper
.
getError
(
e
);
throw
new
CodeException
(
responseResult
.
getCode
(),
responseResult
.
getMessage
(),
responseResult
.
getTarget
());
}
}
}
}
}
...
...
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/WebAspect.java
View file @
b1cff2dd
...
@@ -93,17 +93,22 @@ public class WebAspect extends AbstractValidateAspect implements ThreadNext.Next
...
@@ -93,17 +93,22 @@ public class WebAspect extends AbstractValidateAspect implements ThreadNext.Next
long
start
=
System
.
currentTimeMillis
();
long
start
=
System
.
currentTimeMillis
();
String
name
=
joinPoint
.
getSignature
().
getName
();
String
name
=
joinPoint
.
getSignature
().
getName
();
try
{
try
{
logger
.
info
(
"=================[ {} ] request params is {} ================="
,
name
,
joinPoint
.
getArgs
());
logger
.
info
(
"[ {} ] request params is {}"
,
name
,
joinPoint
.
getArgs
());
Object
result
=
executeMethod
(
joinPoint
,
name
);
Object
result
=
executeMethod
(
joinPoint
,
name
);
if
(
result
instanceof
ResponseResult
)
{
if
(
result
instanceof
ResponseResult
)
{
responseResult
=
(
ResponseResult
)
result
;
responseResult
=
(
ResponseResult
)
result
;
}
else
{
}
else
{
responseResult
=
ResponseResult
.
result
(
result
);
responseResult
=
ResponseResult
.
result
(
result
);
}
}
long
end
=
System
.
currentTimeMillis
();
logger
.
info
(
"[ {} ] time ({})ms, result is {}"
,
name
,
(
end
-
start
),
responseResult
);
return
result
;
return
result
;
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
lo
gger
.
error
(
"=================[ "
+
name
+
" ] is error {} ================="
,
e
);
lo
ng
end
=
System
.
currentTimeMillis
(
);
logger
.
error
(
"[ {} ] time ({})ms, is error {}"
,
name
,
(
end
-
start
),
e
);
responseResult
=
ExceptionHelper
.
getError
(
e
);
responseResult
=
ExceptionHelper
.
getError
(
e
);
if
(
getReturnType
(
joinPoint
).
getTypeName
().
indexOf
(
ResponseResult
.
class
.
getName
())
>
-
1
)
{
if
(
getReturnType
(
joinPoint
).
getTypeName
().
indexOf
(
ResponseResult
.
class
.
getName
())
>
-
1
)
{
return
responseResult
;
return
responseResult
;
...
@@ -111,10 +116,8 @@ public class WebAspect extends AbstractValidateAspect implements ThreadNext.Next
...
@@ -111,10 +116,8 @@ public class WebAspect extends AbstractValidateAspect implements ThreadNext.Next
throw
e
;
throw
e
;
}
}
}
finally
{
}
finally
{
long
end
=
System
.
currentTimeMillis
();
Log
.
threadCommit
();
Log
.
threadCommit
();
saveInterLogs
(
joinPoint
,
responseResult
);
saveInterLogs
(
joinPoint
,
responseResult
);
logger
.
info
(
"=================[ {} ] time ({})ms, result is {} ================="
,
name
,
(
end
-
start
),
responseResult
);
}
}
}
}
...
...
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