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
77a0545b
Commit
77a0545b
authored
May 13, 2024
by
周田
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(response): 统一返回结果
parent
9cd130e6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
152 additions
and
0 deletions
+152
-0
UserController.java
...java/org/linker/springboot/controller/UserController.java
+18
-0
CommonResult.java
...main/java/org/linker/springboot/core/vo/CommonResult.java
+103
-0
GlobalResponseBodyHandler.java
...linker/springboot/core/web/GlobalResponseBodyHandler.java
+31
-0
No files found.
src/main/java/org/linker/springboot/controller/UserController.java
View file @
77a0545b
package
org
.
linker
.
springboot
.
controller
;
package
org
.
linker
.
springboot
.
controller
;
import
org.linker.springboot.core.vo.CommonResult
;
import
org.linker.springboot.dto.UserAddDTO
;
import
org.linker.springboot.dto.UserAddDTO
;
import
org.linker.springboot.dto.UserUpdateDTO
;
import
org.linker.springboot.dto.UserUpdateDTO
;
import
org.linker.springboot.service.UserService
;
import
org.linker.springboot.service.UserService
;
...
@@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.*;
...
@@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.*;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
java.util.UUID
;
/**
/**
* 用户 Controller
* 用户 Controller
...
@@ -104,4 +106,20 @@ public class UserController {
...
@@ -104,4 +106,20 @@ public class UserController {
return
success
;
return
success
;
}
}
/**
* 获得指定用户编号的用户
*
* 提供使用 CommonResult 包装
*
* @param id 用户编号
* @return 用户
*/
@GetMapping
(
"/getWithCommonResult"
)
public
CommonResult
<
UserVO
>
getWithCommonResult
(
@RequestParam
(
"id"
)
Integer
id
)
{
// 查询用户
UserVO
user
=
new
UserVO
().
setId
(
id
).
setUsername
(
UUID
.
randomUUID
().
toString
());
// 返回结果
return
CommonResult
.
success
(
user
);
}
}
}
src/main/java/org/linker/springboot/core/vo/CommonResult.java
0 → 100644
View file @
77a0545b
package
org
.
linker
.
springboot
.
core
.
vo
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
import
org.springframework.util.Assert
;
import
java.io.Serializable
;
/**
* 通用返回结果
*
* @param <T> 结果泛型
*/
public
class
CommonResult
<
T
>
implements
Serializable
{
public
static
Integer
CODE_SUCCESS
=
0
;
/**
* 错误码
*/
private
Integer
code
;
/**
* 错误提示
*/
private
String
message
;
/**
* 返回数据
*/
private
T
data
;
/**
* 将传入的 result 对象,转换成另外一个泛型结果的对象
*
* 因为 A 方法返回的 CommonResult 对象,不满足调用其的 B 方法的返回,所以需要进行转换。
*
* @param result 传入的 result 对象
* @param <T> 返回的泛型
* @return 新的 CommonResult 对象
*/
public
static
<
T
>
CommonResult
<
T
>
error
(
CommonResult
<?>
result
)
{
return
error
(
result
.
getCode
(),
result
.
getMessage
());
}
public
static
<
T
>
CommonResult
<
T
>
error
(
Integer
code
,
String
message
)
{
Assert
.
isTrue
(!
CODE_SUCCESS
.
equals
(
code
),
"code 必须是错误的!"
);
CommonResult
<
T
>
result
=
new
CommonResult
<>();
result
.
code
=
code
;
result
.
message
=
message
;
return
result
;
}
public
static
<
T
>
CommonResult
<
T
>
success
(
T
data
)
{
CommonResult
<
T
>
result
=
new
CommonResult
<>();
result
.
code
=
CODE_SUCCESS
;
result
.
data
=
data
;
result
.
message
=
""
;
return
result
;
}
public
Integer
getCode
()
{
return
code
;
}
public
void
setCode
(
Integer
code
)
{
this
.
code
=
code
;
}
public
String
getMessage
()
{
return
message
;
}
public
void
setMessage
(
String
message
)
{
this
.
message
=
message
;
}
public
T
getData
()
{
return
data
;
}
public
void
setData
(
T
data
)
{
this
.
data
=
data
;
}
@JsonIgnore
public
boolean
isSuccess
()
{
return
CODE_SUCCESS
.
equals
(
code
);
}
@JsonIgnore
public
boolean
isError
()
{
return
!
isSuccess
();
}
@Override
public
String
toString
()
{
return
"CommonResult{"
+
"code="
+
code
+
", message='"
+
message
+
'\''
+
", data="
+
data
+
'}'
;
}
}
\ No newline at end of file
src/main/java/org/linker/springboot/core/web/GlobalResponseBodyHandler.java
0 → 100644
View file @
77a0545b
package
org
.
linker
.
springboot
.
core
.
web
;
import
org.linker.springboot.core.vo.CommonResult
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.server.ServerHttpRequest
;
import
org.springframework.http.server.ServerHttpResponse
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice
;
// 只拦截我们的 Controller 所在包,避免其它类似 swagger 提供的 API 被切面拦截
@ControllerAdvice
(
basePackages
=
"org.linker.springboot.controller"
)
public
class
GlobalResponseBodyHandler
implements
ResponseBodyAdvice
{
@Override
public
boolean
supports
(
MethodParameter
returnType
,
Class
converterType
)
{
return
true
;
}
@Override
public
Object
beforeBodyWrite
(
Object
body
,
MethodParameter
returnType
,
MediaType
selectedContentType
,
Class
selectedConverterType
,
ServerHttpRequest
request
,
ServerHttpResponse
response
)
{
// 如果已经是 CommonResult 类型,则直接返回
if
(
body
instanceof
CommonResult
)
{
return
body
;
}
// 如果不是,则包装成 CommonResult 类型
return
CommonResult
.
success
(
body
);
}
}
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