File tree Expand file tree Collapse file tree 5 files changed +22
-9
lines changed
main/java/ml/sun/controller
test/java/ml/sun/controller
common/src/main/java/ml/sun/common/advice Expand file tree Collapse file tree 5 files changed +22
-9
lines changed Original file line number Diff line number Diff line change @@ -2,15 +2,15 @@ name: deploy site to github pages
2
2
3
3
on :
4
4
push :
5
- branches : master
5
+ branches : main
6
6
7
7
jobs :
8
8
build :
9
9
runs-on : ubuntu-latest
10
10
env :
11
11
PAGES_PATH : docs/.vitepress/dist
12
12
REPO : https://github.com/SunYufei/sunyufei.github.io.git
13
- BRANCH : master
13
+ BRANCH : main
14
14
steps :
15
15
- uses : actions/checkout@v4
16
16
with :
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package ml.sun.controller
2
2
3
3
import ml.sun.common.result.BaseResult
4
4
import ml.sun.common.result.ResultCode
5
+ import org.springframework.http.MediaType
5
6
import org.springframework.web.bind.annotation.GetMapping
6
7
import org.springframework.web.bind.annotation.RequestMapping
7
8
import org.springframework.web.bind.annotation.RequestParam
@@ -18,8 +19,13 @@ class HealthController {
18
19
BaseResult .success(param)
19
20
20
21
@GetMapping(" error" )
21
- fun error () = 1 / 0
22
+ fun error () {
23
+ throw ArithmeticException ()
24
+ }
22
25
23
26
@GetMapping(" body" )
24
27
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>"
25
31
}
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import ml.sun.common.result.ResultCode
4
4
import org.junit.jupiter.api.Test
5
5
import org.springframework.beans.factory.annotation.Autowired
6
6
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
7
+ import org.springframework.http.MediaType
7
8
import org.springframework.test.web.servlet.MockMvc
8
9
import org.springframework.test.web.servlet.get
9
10
@@ -41,5 +42,10 @@ class HealthControllerTest {
41
42
status { isOk() }
42
43
jsonPath(" $.data" ) { value(100 ) }
43
44
}.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 () }
44
50
}
45
51
}
Original file line number Diff line number Diff line change 1
1
package ml.sun.common.advice
2
2
3
- import ml.sun.common.ext.JsonExt.toJson
4
3
import ml.sun.common.result.BaseResult
5
4
import org.springframework.core.MethodParameter
6
5
import org.springframework.http.MediaType
@@ -26,9 +25,11 @@ class ResponseAdvice : ResponseBodyAdvice<Any> {
26
25
selectedConverterType : Class <out HttpMessageConverter <* >>,
27
26
request : ServerHttpRequest ,
28
27
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)
33
34
}
34
35
}
Original file line number Diff line number Diff line change 6
6
<parent >
7
7
<groupId >org.springframework.boot</groupId >
8
8
<artifactId >spring-boot-starter-parent</artifactId >
9
- <version >3.2.3 </version >
9
+ <version >3.2.5 </version >
10
10
<relativePath />
11
11
</parent >
12
12
You can’t perform that action at this time.
0 commit comments