Skip to content

Commit 74a0c00

Browse files
committed
[spring boot] 统一返回结果优化
1 parent 20bc5d5 commit 74a0c00

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

.github/workflows/pages.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ name: deploy site to github pages
22

33
on:
44
push:
5-
branches: master
5+
branches: main
66

77
jobs:
88
build:
99
runs-on: ubuntu-latest
1010
env:
1111
PAGES_PATH: docs/.vitepress/dist
1212
REPO: https://github.com/SunYufei/sunyufei.github.io.git
13-
BRANCH: master
13+
BRANCH: main
1414
steps:
1515
- uses: actions/checkout@v4
1616
with:

spring-boot-demo/api/src/main/java/ml/sun/controller/HealthController.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ml.sun.controller
22

33
import ml.sun.common.result.BaseResult
44
import ml.sun.common.result.ResultCode
5+
import org.springframework.http.MediaType
56
import org.springframework.web.bind.annotation.GetMapping
67
import org.springframework.web.bind.annotation.RequestMapping
78
import org.springframework.web.bind.annotation.RequestParam
@@ -18,8 +19,13 @@ class HealthController {
1819
BaseResult.success(param)
1920

2021
@GetMapping("error")
21-
fun error() = 1 / 0
22+
fun error() {
23+
throw ArithmeticException()
24+
}
2225

2326
@GetMapping("body")
2427
fun body(@RequestParam code: Int) = code
28+
29+
@GetMapping(path = ["html"], produces = [MediaType.TEXT_HTML_VALUE])
30+
fun html() = "<html><header><title>HTML</title></header><body><h1>H1</h1></body></html>"
2531
}

spring-boot-demo/api/src/test/java/ml/sun/controller/HealthControllerTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ml.sun.common.result.ResultCode
44
import org.junit.jupiter.api.Test
55
import org.springframework.beans.factory.annotation.Autowired
66
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
7+
import org.springframework.http.MediaType
78
import org.springframework.test.web.servlet.MockMvc
89
import org.springframework.test.web.servlet.get
910

@@ -41,5 +42,10 @@ class HealthControllerTest {
4142
status { isOk() }
4243
jsonPath("$.data") { value(100) }
4344
}.andDo { print() }
45+
46+
mockMvc.get("/health/html").andExpect {
47+
status { isOk() }
48+
content { contentType("${MediaType.TEXT_HTML_VALUE};charset=UTF-8") }
49+
}.andDo { print() }
4450
}
4551
}

spring-boot-demo/common/src/main/java/ml/sun/common/advice/ResponseAdvice.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package ml.sun.common.advice
22

3-
import ml.sun.common.ext.JsonExt.toJson
43
import ml.sun.common.result.BaseResult
54
import org.springframework.core.MethodParameter
65
import org.springframework.http.MediaType
@@ -26,9 +25,11 @@ class ResponseAdvice : ResponseBodyAdvice<Any> {
2625
selectedConverterType: Class<out HttpMessageConverter<*>>,
2726
request: ServerHttpRequest,
2827
response: ServerHttpResponse
29-
): Any? {
30-
val result = BaseResult.success(body)
31-
// String 类型转换为 JSON 字符串
32-
return if (body is String) result.toJson() else result
28+
): Any? = if (selectedContentType != MediaType.APPLICATION_JSON) {
29+
// 非 JSON 返回不作处理
30+
body
31+
} else {
32+
// JSON 返回包装返回
33+
BaseResult.success(body)
3334
}
3435
}

spring-boot-demo/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>3.2.3</version>
9+
<version>3.2.5</version>
1010
<relativePath/>
1111
</parent>
1212

0 commit comments

Comments
 (0)