Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
springboot-study
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
周田
springboot-study
Commits
a4477650
Commit
a4477650
authored
May 15, 2024
by
周田
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(response): 全局异常处理
parent
77a0545b
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
163 additions
and
1 deletions
+163
-1
ServiceExceptionEnum.java
...org/linker/springboot/constants/ServiceExceptionEnum.java
+44
-0
UserController.java
...java/org/linker/springboot/controller/UserController.java
+18
-0
ServiceException.java
...rg/linker/springboot/core/exception/ServiceException.java
+23
-0
GlobalExceptionHandler.java
...rg/linker/springboot/core/web/GlobalExceptionHandler.java
+59
-0
application.properties
src/main/resources/application.properties
+0
-1
application.yml
src/main/resources/application.yml
+3
-0
rebel.xml
src/main/resources/rebel.xml
+16
-0
No files found.
src/main/java/org/linker/springboot/constants/ServiceExceptionEnum.java
0 → 100644
View file @
a4477650
package
org
.
linker
.
springboot
.
constants
;
/**
* 业务异常枚举
*/
public
enum
ServiceExceptionEnum
{
// ========== 系统级别 ==========
SUCCESS
(
0
,
"成功"
),
SYS_ERROR
(
2001001000
,
"服务端发生异常"
),
MISSING_REQUEST_PARAM_ERROR
(
2001001001
,
"参数缺失"
),
// ========== 用户模块 ==========
USER_NOT_FOUND
(
1001002000
,
"用户不存在"
),
// ========== 订单模块 ==========
// ========== 商品模块 ==========
;
/**
* 错误码
*/
private
final
int
code
;
/**
* 错误提示
*/
private
final
String
message
;
ServiceExceptionEnum
(
int
code
,
String
message
)
{
this
.
code
=
code
;
this
.
message
=
message
;
}
public
int
getCode
()
{
return
code
;
}
public
String
getMessage
()
{
return
message
;
}
}
\ No newline at end of file
src/main/java/org/linker/springboot/controller/UserController.java
View file @
a4477650
package
org
.
linker
.
springboot
.
controller
;
import
org.linker.springboot.constants.ServiceExceptionEnum
;
import
org.linker.springboot.core.exception.ServiceException
;
import
org.linker.springboot.core.vo.CommonResult
;
import
org.linker.springboot.dto.UserAddDTO
;
import
org.linker.springboot.dto.UserUpdateDTO
;
...
...
@@ -122,4 +124,20 @@ public class UserController {
return
CommonResult
.
success
(
user
);
}
/**
* 测试抛出 NullPointerException 异常
*/
@GetMapping
(
"/exception-01"
)
public
UserVO
exception01
()
{
throw
new
NullPointerException
(
"没有粗面鱼丸"
);
}
/**
* 测试抛出 ServiceException 异常
*/
@GetMapping
(
"/exception-02"
)
public
UserVO
exception02
()
{
throw
new
ServiceException
(
ServiceExceptionEnum
.
USER_NOT_FOUND
);
}
}
src/main/java/org/linker/springboot/core/exception/ServiceException.java
0 → 100644
View file @
a4477650
package
org
.
linker
.
springboot
.
core
.
exception
;
import
org.linker.springboot.constants.ServiceExceptionEnum
;
public
final
class
ServiceException
extends
RuntimeException
{
/**
* 错误码
*/
private
final
Integer
code
;
public
ServiceException
(
ServiceExceptionEnum
serviceExceptionEnum
)
{
// 使用父类的 message 字段
super
(
serviceExceptionEnum
.
getMessage
());
// 设置错误码
this
.
code
=
serviceExceptionEnum
.
getCode
();
}
public
Integer
getCode
()
{
return
code
;
}
}
src/main/java/org/linker/springboot/core/web/GlobalExceptionHandler.java
0 → 100644
View file @
a4477650
package
org
.
linker
.
springboot
.
core
.
web
;
import
jakarta.servlet.http.HttpServletRequest
;
import
org.linker.springboot.core.exception.ServiceException
;
import
org.linker.springboot.constants.ServiceExceptionEnum
;
import
org.linker.springboot.core.vo.CommonResult
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.web.bind.MissingServletRequestParameterException
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
@ControllerAdvice
(
basePackages
=
"org.linker.springboot.controller"
)
public
class
GlobalExceptionHandler
{
private
Logger
logger
=
LoggerFactory
.
getLogger
(
getClass
());
/**
* 处理 ServiceException 异常
*/
@ResponseBody
@ExceptionHandler
(
value
=
ServiceException
.
class
)
public
CommonResult
serviceExceptionHandler
(
HttpServletRequest
req
,
ServiceException
ex
)
{
logger
.
debug
(
"[serviceExceptionHandler]"
,
ex
);
// 包装 CommonResult 结果
return
CommonResult
.
error
(
ex
.
getCode
(),
ex
.
getMessage
());
}
/**
* 处理 MissingServletRequestParameterException 异常
* <p>
* SpringMVC 参数不正确
*/
@ResponseBody
@ExceptionHandler
(
value
=
MissingServletRequestParameterException
.
class
)
public
CommonResult
missingServletRequestParameterExceptionHandler
(
HttpServletRequest
req
,
MissingServletRequestParameterException
ex
)
{
logger
.
debug
(
"[missingServletRequestParameterExceptionHandler]"
,
ex
);
// 包装 CommonResult 结果
return
CommonResult
.
error
(
ServiceExceptionEnum
.
MISSING_REQUEST_PARAM_ERROR
.
getCode
(),
ServiceExceptionEnum
.
MISSING_REQUEST_PARAM_ERROR
.
getMessage
());
}
/**
* 处理其它 Exception 异常
*/
@ResponseBody
@ExceptionHandler
(
value
=
Exception
.
class
)
public
CommonResult
exceptionHandler
(
HttpServletRequest
req
,
Exception
e
)
{
// 记录异常日志
logger
.
error
(
"[exceptionHandler]"
,
e
);
// 返回 ERROR CommonResult
return
CommonResult
.
error
(
ServiceExceptionEnum
.
SYS_ERROR
.
getCode
(),
ServiceExceptionEnum
.
SYS_ERROR
.
getMessage
());
}
}
src/main/resources/application.properties
deleted
100644 → 0
View file @
77a0545b
spring.application.name
=
springboot
src/main/resources/application.yml
0 → 100644
View file @
a4477650
spring
:
application
:
name
:
springboot
src/main/resources/rebel.xml
0 → 100644
View file @
a4477650
<?xml version="1.0" encoding="UTF-8"?>
<!--
This is the JRebel configuration file. It maps the running application to your IDE workspace, enabling JRebel reloading for this project.
Refer to https://manuals.jrebel.com/jrebel/standalone/config.html for more information.
-->
<application
generated-by=
"intellij"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns=
"http://www.zeroturnaround.com"
xsi:schemaLocation=
"http://www.zeroturnaround.com http://update.zeroturnaround.com/jrebel/rebel-2_3.xsd"
>
<id>
springboot
</id>
<classpath>
<dir
name=
"/Users/zutjoe/code/spring-study/springboot/target/classes"
>
</dir>
</classpath>
</application>
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