Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
open-source
/
chejia-smart-api
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 94ad1af3
authored
Mar 18, 2021
by
tyfanchz
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
1、功能开发
2、细化统一异常处理方法 3、去除无用校验
1 parent
7723a017
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
1 deletions
src/main/java/com/glsx/api/controller/ProductTypeController.java
src/main/java/com/glsx/api/exception/GlobalExceptionHandler.java
src/main/java/com/glsx/api/exception/ResultEnum.java
src/main/java/com/glsx/api/controller/ProductTypeController.java
View file @
94ad1af
...
@@ -29,7 +29,7 @@ public class ProductTypeController {
...
@@ -29,7 +29,7 @@ public class ProductTypeController {
* @return 结果
* @return 结果
*/
*/
@RequestMapping
(
value
=
"/getTypeAndCategory"
,
method
=
{
RequestMethod
.
GET
,
RequestMethod
.
POST
})
@RequestMapping
(
value
=
"/getTypeAndCategory"
,
method
=
{
RequestMethod
.
GET
,
RequestMethod
.
POST
})
public
R
get
ProductTypeTree
()
{
public
R
get
TypeAndCategory
()
{
log
.
info
(
"$$$$ /productType/getTypeAndCategory..."
);
log
.
info
(
"$$$$ /productType/getTypeAndCategory..."
);
JSONObject
result
=
productTypeFeignClient
.
getTypeAndCategory
();
JSONObject
result
=
productTypeFeignClient
.
getTypeAndCategory
();
return
R
.
ok
().
data
(
result
);
return
R
.
ok
().
data
(
result
);
...
...
src/main/java/com/glsx/api/exception/GlobalExceptionHandler.java
View file @
94ad1af
package
com
.
glsx
.
api
.
exception
;
package
com
.
glsx
.
api
.
exception
;
import
java.util.List
;
import
java.util.concurrent.TimeoutException
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.glsx.plat.common.utils.StringUtils
;
import
com.glsx.plat.common.utils.StringUtils
;
...
@@ -8,6 +10,7 @@ import com.netflix.hystrix.exception.HystrixRuntimeException;
...
@@ -8,6 +10,7 @@ import com.netflix.hystrix.exception.HystrixRuntimeException;
import
feign.FeignException
;
import
feign.FeignException
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.FieldError
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.ServletRequestBindingException
;
import
org.springframework.web.bind.ServletRequestBindingException
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
...
@@ -33,8 +36,27 @@ public class GlobalExceptionHandler {
...
@@ -33,8 +36,27 @@ public class GlobalExceptionHandler {
BindException
.
class
})
BindException
.
class
})
public
R
handleBindException
(
Exception
e
)
{
public
R
handleBindException
(
Exception
e
)
{
log
.
error
(
"!!!! 参数错误"
,
e
);
log
.
error
(
"!!!! 参数错误"
,
e
);
if
((
e
instanceof
MethodArgumentNotValidException
)
||
(
e
instanceof
BindException
))
{
List
<
FieldError
>
errorList
;
if
(
e
instanceof
MethodArgumentNotValidException
)
{
MethodArgumentNotValidException
ex
=
(
MethodArgumentNotValidException
)
e
;
errorList
=
ex
.
getBindingResult
().
getFieldErrors
();
}
else
{
BindException
ex
=
(
BindException
)
e
;
errorList
=
ex
.
getFieldErrors
();
}
StringBuffer
messageSb
=
new
StringBuffer
();
for
(
FieldError
error
:
errorList
)
{
String
fieldMessage
=
String
.
format
(
"%s: %s, "
,
error
.
getField
(),
error
.
getDefaultMessage
());
messageSb
.
append
(
fieldMessage
);
}
return
R
.
error
(
ResultEnum
.
PARAM_ERROR
.
getCode
(),
messageSb
.
toString
().
replaceAll
(
", +$"
,
""
));
}
else
{
return
R
.
error
(
ResultEnum
.
PARAM_ERROR
.
getCode
(),
ResultEnum
.
PARAM_ERROR
.
getMessage
());
return
R
.
error
(
ResultEnum
.
PARAM_ERROR
.
getCode
(),
ResultEnum
.
PARAM_ERROR
.
getMessage
());
}
}
}
@ExceptionHandler
({
HystrixRuntimeException
.
class
})
@ExceptionHandler
({
HystrixRuntimeException
.
class
})
public
R
handleHystrixException
(
HystrixRuntimeException
e
)
{
public
R
handleHystrixException
(
HystrixRuntimeException
e
)
{
...
@@ -42,6 +64,10 @@ public class GlobalExceptionHandler {
...
@@ -42,6 +64,10 @@ public class GlobalExceptionHandler {
if
(
cause
instanceof
FeignException
)
{
if
(
cause
instanceof
FeignException
)
{
FeignException
feignException
=
(
FeignException
)
cause
;
FeignException
feignException
=
(
FeignException
)
cause
;
return
this
.
handleFeignException
(
feignException
);
return
this
.
handleFeignException
(
feignException
);
}
else
if
(
cause
instanceof
TimeoutException
)
{
log
.
error
(
"!!!! 服务调用超时"
,
e
);
return
R
.
error
(
ResultEnum
.
REMOTE_SERVER_TIME_OUT
.
getCode
(),
ResultEnum
.
REMOTE_SERVER_TIME_OUT
.
getMessage
());
}
else
{
}
else
{
log
.
error
(
"!!!! Hystrix熔断错误"
,
e
);
log
.
error
(
"!!!! Hystrix熔断错误"
,
e
);
return
R
.
error
(
ResultEnum
.
ERROR
.
getCode
(),
ResultEnum
.
ERROR
.
getMessage
());
return
R
.
error
(
ResultEnum
.
ERROR
.
getCode
(),
ResultEnum
.
ERROR
.
getMessage
());
...
...
src/main/java/com/glsx/api/exception/ResultEnum.java
View file @
94ad1af
...
@@ -19,6 +19,7 @@ public enum ResultEnum {
...
@@ -19,6 +19,7 @@ public enum ResultEnum {
PRODUCT_TYPE_NOT_FOUND
(
3001
,
"产品类型ID未找到"
),
PRODUCT_TYPE_NOT_FOUND
(
3001
,
"产品类型ID未找到"
),
REMOTE_SERVICE_ERROR
(
9001
,
"服务调用错误"
),
REMOTE_SERVICE_ERROR
(
9001
,
"服务调用错误"
),
REMOTE_SERVER_TIME_OUT
(
9002
,
"远程服务超时,请重试"
),
;
;
private
final
Integer
code
;
private
final
Integer
code
;
...
...
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