Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
indicators
Overview
Overview
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
zhangkb
indicators
Commits
f173dbb5
Commit
f173dbb5
authored
Apr 17, 2020
by
张祺
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加异常拦截
parent
2ff61850
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
290 additions
and
4 deletions
+290
-4
pom.xml
pom.xml
+4
-4
ApiError.java
...ain/java/com/keymobile/indicators/exception/ApiError.java
+56
-0
RestExceptionHandler.java
.../keymobile/indicators/exception/RestExceptionHandler.java
+34
-0
Result.java
src/main/java/com/keymobile/indicators/result/Result.java
+108
-0
ResultCodeEnum.java
.../java/com/keymobile/indicators/result/ResultCodeEnum.java
+67
-0
RespCode.java
src/main/java/com/keymobile/indicators/utils/RespCode.java
+21
-0
No files found.
pom.xml
View file @
f173dbb5
...
...
@@ -23,10 +23,10 @@
</properties>
<dependencies>
<
dependency
>
<
groupId>
org.springframework.boot
</groupId
>
<
artifactId>
spring-boot-starter-data-mongodb
</artifactId
>
<
/dependency
>
<
!--<dependency>--
>
<
!--<groupId>org.springframework.boot</groupId>--
>
<
!--<artifactId>spring-boot-starter-data-mongodb</artifactId>--
>
<
!--</dependency>--
>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
...
...
src/main/java/com/keymobile/indicators/exception/ApiError.java
0 → 100644
View file @
f173dbb5
package
com
.
keymobile
.
indicators
.
exception
;
import
com.fasterxml.jackson.annotation.JsonTypeInfo
;
import
org.springframework.http.HttpStatus
;
@JsonTypeInfo
(
include
=
JsonTypeInfo
.
As
.
WRAPPER_OBJECT
,
use
=
JsonTypeInfo
.
Id
.
NAME
,
property
=
"error"
,
visible
=
true
)
class
ApiError
{
private
HttpStatus
status
;
private
Long
timestamp
;
private
String
message
;
private
String
cnMessage
=
"网络异常"
;
private
ApiError
()
{
timestamp
=
System
.
currentTimeMillis
();
}
ApiError
(
HttpStatus
status
)
{
this
();
this
.
status
=
status
;
}
ApiError
(
HttpStatus
status
,
Throwable
ex
)
{
this
();
this
.
status
=
status
;
}
ApiError
(
HttpStatus
status
,
String
message
,
Throwable
ex
)
{
this
();
this
.
status
=
status
;
this
.
message
=
message
;
}
ApiError
(
HttpStatus
status
,
String
message
,
String
cnMessage
,
Throwable
ex
)
{
this
();
this
.
status
=
status
;
this
.
message
=
message
;
this
.
cnMessage
=
cnMessage
;
}
public
HttpStatus
getStatus
()
{
return
status
;
}
public
Long
getTimestamp
()
{
return
timestamp
;
}
public
String
getMessage
()
{
return
message
;
}
public
String
getCnMessage
()
{
return
cnMessage
;
}
}
src/main/java/com/keymobile/indicators/exception/RestExceptionHandler.java
0 → 100644
View file @
f173dbb5
package
com
.
keymobile
.
indicators
.
exception
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.dao.DataIntegrityViolationException
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.context.request.WebRequest
;
import
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
;
import
static
org
.
springframework
.
http
.
HttpStatus
.
INTERNAL_SERVER_ERROR
;
@Order
(
Ordered
.
HIGHEST_PRECEDENCE
)
@ControllerAdvice
public
class
RestExceptionHandler
extends
ResponseEntityExceptionHandler
{
@ExceptionHandler
(
Exception
.
class
)
protected
ResponseEntity
<
Object
>
handlException
(
Exception
ex
,
WebRequest
request
)
{
ApiError
apiError
;
if
(
ex
instanceof
DataIntegrityViolationException
)
{
apiError
=
new
ApiError
(
INTERNAL_SERVER_ERROR
,
ex
.
getMessage
(),
"重复值"
,
ex
);
}
else
{
apiError
=
new
ApiError
(
INTERNAL_SERVER_ERROR
,
ex
.
getMessage
(),
ex
);
}
return
buildResponseEntity
(
apiError
);
}
private
ResponseEntity
<
Object
>
buildResponseEntity
(
ApiError
apiError
)
{
return
new
ResponseEntity
<>(
apiError
,
apiError
.
getStatus
());
}
}
src/main/java/com/keymobile/indicators/result/Result.java
0 → 100644
View file @
f173dbb5
package
com
.
keymobile
.
indicators
.
result
;
import
java.io.Serializable
;
/**
* 统一结果对象
* @param <T>
*/
@SuppressWarnings
(
"rawtypes"
)
public
class
Result
<
T
>
implements
Serializable
{
private
static
final
String
DEFAULT_UNAUTHORIZED_MESSAGE
=
"Need authorized"
;
private
static
final
String
DEFAULT_METHOD_NOT_ALLOWED_MESSAGE
=
"Request method incorrect"
;
/**
* 结果编码
*/
private
int
code
;
/**
* 结果消息内容
*/
private
String
msg
;
/**
* 结果数据对象
*/
private
T
data
;
public
Result
(
T
data
)
{
this
(
ResultCodeEnum
.
SUCCESS
,
data
);
}
public
Result
(
ResultCodeEnum
codeEnum
)
{
this
(
codeEnum
.
getCode
(),
codeEnum
.
getMsg
(),
null
);
}
public
Result
(
ResultCodeEnum
codeEnum
,
T
data
)
{
this
(
codeEnum
.
getCode
(),
codeEnum
.
getMsg
(),
data
);
}
public
Result
(
ResultCodeEnum
codeEnum
,
String
msg
,
T
data
)
{
this
(
codeEnum
.
getCode
(),
msg
,
data
);
}
public
Result
(
int
code
,
String
msg
,
T
data
)
{
this
.
code
=
code
;
this
.
msg
=
msg
;
this
.
data
=
data
;
}
public
Integer
getCode
()
{
return
code
;
}
public
String
getMsg
()
{
return
msg
;
}
public
T
getData
()
{
return
data
;
}
public
boolean
isSuccess
()
{
return
this
.
code
==
ResultCodeEnum
.
SUCCESS
.
getCode
();
}
@Override
public
String
toString
()
{
return
"Result{"
+
"code="
+
code
+
", msg='"
+
msg
+
'\''
+
", data="
+
data
+
'}'
;
}
public
static
<
T
>
Result
<
T
>
genOkResult
()
{
return
new
Result
<
T
>(
ResultCodeEnum
.
SUCCESS
,
null
);
}
public
static
Result
genOkResult
(
final
Object
data
)
{
return
new
Result
<
Object
>(
ResultCodeEnum
.
SUCCESS
,
data
);
}
public
static
Result
genFailedResult
(
final
String
msg
)
{
return
new
Result
<
Object
>(
ResultCodeEnum
.
OPERATION_FAIL
,
msg
,
msg
);
}
public
static
Result
genOteherCodeOkResult
(
final
Object
data
,
ResultCodeEnum
resultCodeEnum
)
{
return
new
Result
<
Object
>(
resultCodeEnum
,
data
);
}
public
static
Result
genMethodErrorResult
()
{
return
new
Result
<
Object
>(
ResultCodeEnum
.
METHOD_NOT_ALLOWED
,
DEFAULT_METHOD_NOT_ALLOWED_MESSAGE
);
}
public
static
Result
genUnauthorizedResult
()
{
return
new
Result
<
Object
>(
ResultCodeEnum
.
UNAUTHORIZED
,
DEFAULT_UNAUTHORIZED_MESSAGE
);
}
public
static
Result
genUnauthorizedResult
(
final
String
msg
)
{
return
new
Result
<
Object
>(
ResultCodeEnum
.
UNAUTHORIZED
,
msg
);
}
public
static
Result
genInternalServerErrorResult
(
final
String
url
)
{
return
new
Result
<
Object
>(
ResultCodeEnum
.
SYSTEM_ERROR
,
"API ["
+
url
+
"] internal server error. Please call engineer to debug."
);
}
}
src/main/java/com/keymobile/indicators/result/ResultCodeEnum.java
0 → 100644
View file @
f173dbb5
package
com
.
keymobile
.
indicators
.
result
;
/**
* 返回结果编码枚举定义
*/
public
enum
ResultCodeEnum
{
// 响应码定义
// ====== 通用状态码 ======
// 正常
SUCCESS
(
0
,
"OK"
),
// 系统异常
EXCEPTION
(-
1
,
"System Exception"
),
// 连接失败
CONN_ERR
(-
2
,
"Connection Error Or Timeout"
),
// 参数缺失
PARAM_EMPTY
(-
3
,
"Param Is Null Or Empty"
),
// 参数异常
PARAM_ILLEGAL
(-
4
,
"Param Illegal"
),
// 操作失败
OPERATION_FAIL
(-
5
,
"Operation Fail"
),
// 参数验证
NOT_NULL
(-
5
,
"%s 不能为null value:null"
),
INVALID_TYPE
(-
5
,
"%s 参数类型不正确 value:%s"
),
INVALID_PARAM
(-
5
,
"%s 参数不正确 value:%s"
),
INVALID_BODY_PARAM
(-
5
,
"请求体不正确:%s"
),
OCCUPATION_CLOSED
(
10001
,
"所查占用已被临时关闭"
),
SUCCESS_200
(
200
,
"ok"
),
REQUEST_ERROR
(
400
,
"Param Error"
),
UNAUTHORIZED
(
401
,
"Unauthorized"
),
METHOD_NOT_ALLOWED
(
405
,
"Method Not Allowed"
),
NOT_FOUND
(
404
,
"404 Not Found"
),
DATA_ERR
(
420
,
"data err"
),
SYSTEM_ERROR
(
500
,
"System Error"
);
private
final
int
code
;
private
final
String
msg
;
ResultCodeEnum
(
int
code
,
String
msg
)
{
this
.
code
=
code
;
this
.
msg
=
msg
;
}
public
int
getCode
()
{
return
code
;
}
public
String
getMsg
()
{
return
msg
;
}
public
String
formatMsg
(
Object
...
args
)
{
return
String
.
format
(
msg
,
args
);
}
@Override
public
String
toString
()
{
return
"ResultCodeEnum{"
+
"code="
+
code
+
", msg='"
+
msg
+
'\''
+
'}'
;
}
}
src/main/java/com/keymobile/indicators/utils/RespCode.java
0 → 100644
View file @
f173dbb5
package
com
.
keymobile
.
indicators
.
utils
;
public
enum
RespCode
{
SUCCESS
(
0
,
"请求成功"
),
FAIL
(-
1
,
"请求失败"
);
private
int
code
;
private
String
msg
;
RespCode
(
int
code
,
String
msg
)
{
this
.
code
=
code
;
this
.
msg
=
msg
;
}
public
int
getCode
()
{
return
code
;
}
public
String
getMsg
()
{
return
msg
;
}
}
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