Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
T
traffic-front
Project
Project
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangxiaoming
traffic-front
Commits
ea5a057f
Commit
ea5a057f
authored
Nov 12, 2018
by
neo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[DEV] exception handler multi
parent
c50d8ab7
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
35 deletions
+21
-35
CustomExceptionHandler.java
...taxtech/atms/common/exception/CustomExceptionHandler.java
+0
-34
AtmsExceptionHandler.java
...ava/pwc/taxtech/atms/controller/AtmsExceptionHandler.java
+21
-1
No files found.
atms-api/src/main/java/pwc/taxtech/atms/common/exception/CustomExceptionHandler.java
deleted
100644 → 0
View file @
c50d8ab7
package
pwc
.
taxtech
.
atms
.
common
.
exception
;
import
com.alibaba.fastjson.JSON
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
pwc.taxtech.atms.common.ServiceException
;
import
pwc.taxtech.atms.dto.ApiResultDto
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
@ControllerAdvice
public
class
CustomExceptionHandler
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
CustomExceptionHandler
.
class
);
@ExceptionHandler
(
value
=
Throwable
.
class
)
public
ApiResultDto
handle
(
HttpServletResponse
response
)
{
return
ApiResultDto
.
fail
(
"error."
);
}
@ExceptionHandler
(
value
=
ServiceException
.
class
)
public
void
customHandle
(
ServiceException
exception
,
HttpServletResponse
response
)
{
try
{
response
.
setCharacterEncoding
(
"UTF-8"
);
response
.
setContentType
(
"application/json; charset=UTF-8"
);
response
.
getWriter
().
write
(
JSON
.
toJSONString
(
ApiResultDto
.
fail
(
exception
.
getMessage
())));
}
catch
(
IOException
e
)
{
logger
.
error
(
"customHandle error."
,
e
);
}
}
}
atms-api/src/main/java/pwc/taxtech/atms/controller/AtmsExceptionHandler.java
View file @
ea5a057f
package
pwc
.
taxtech
.
atms
.
controller
;
import
com.alibaba.fastjson.JSON
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.http.HttpStatus
;
...
...
@@ -8,10 +9,14 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.servlet.config.annotation.EnableWebMvc
;
import
org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
;
import
pwc.taxtech.atms.dto.ApiResultDto
;
import
pwc.taxtech.atms.exception.ApiException
;
import
pwc.taxtech.atms.exception.ApplicationException
;
import
pwc.taxtech.atms.exception.ServiceException
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
@EnableWebMvc
@ControllerAdvice
public
class
AtmsExceptionHandler
extends
ResponseEntityExceptionHandler
{
...
...
@@ -19,7 +24,6 @@ public class AtmsExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler
(
value
=
{
ApplicationException
.
class
,
ServiceException
.
class
,
ApiException
.
class
})
protected
ResponseEntity
<
Object
>
handleExceptions
(
Exception
ex
)
throws
ServiceException
{
...
...
@@ -45,6 +49,22 @@ public class AtmsExceptionHandler extends ResponseEntityExceptionHandler {
}
}
@ExceptionHandler
(
value
=
Throwable
.
class
)
public
ApiResultDto
handle
(
HttpServletResponse
response
)
{
return
ApiResultDto
.
fail
(
"error."
);
}
@ExceptionHandler
(
value
=
ServiceException
.
class
)
public
void
customHandle
(
pwc
.
taxtech
.
atms
.
common
.
ServiceException
exception
,
HttpServletResponse
response
)
{
try
{
response
.
setCharacterEncoding
(
"UTF-8"
);
response
.
setContentType
(
"application/json; charset=UTF-8"
);
response
.
getWriter
().
write
(
JSON
.
toJSONString
(
ApiResultDto
.
fail
(
exception
.
getMessage
())));
}
catch
(
IOException
e
)
{
logger
.
error
(
"customHandle error."
,
e
);
}
}
private
ResponseEntity
<
Object
>
handleApplicationException
(
ApplicationException
ex
)
{
throw
ex
;
}
...
...
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