Skip to content

Commit

Permalink
[spring boot] 统一返回结果优化
Browse files Browse the repository at this point in the history
  • Loading branch information
SunYufei committed May 21, 2024
1 parent 20bc5d5 commit 74a0c00
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: deploy site to github pages

on:
push:
branches: master
branches: main

jobs:
build:
runs-on: ubuntu-latest
env:
PAGES_PATH: docs/.vitepress/dist
REPO: https://github.com/SunYufei/sunyufei.github.io.git
BRANCH: master
BRANCH: main
steps:
- uses: actions/checkout@v4
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ml.sun.controller

import ml.sun.common.result.BaseResult
import ml.sun.common.result.ResultCode
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
Expand All @@ -18,8 +19,13 @@ class HealthController {
BaseResult.success(param)

@GetMapping("error")
fun error() = 1 / 0
fun error() {
throw ArithmeticException()
}

@GetMapping("body")
fun body(@RequestParam code: Int) = code

@GetMapping(path = ["html"], produces = [MediaType.TEXT_HTML_VALUE])
fun html() = "<html><header><title>HTML</title></header><body><h1>H1</h1></body></html>"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ml.sun.common.result.ResultCode
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.get

Expand Down Expand Up @@ -41,5 +42,10 @@ class HealthControllerTest {
status { isOk() }
jsonPath("$.data") { value(100) }
}.andDo { print() }

mockMvc.get("/health/html").andExpect {
status { isOk() }
content { contentType("${MediaType.TEXT_HTML_VALUE};charset=UTF-8") }
}.andDo { print() }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ml.sun.common.advice

import ml.sun.common.ext.JsonExt.toJson
import ml.sun.common.result.BaseResult
import org.springframework.core.MethodParameter
import org.springframework.http.MediaType
Expand All @@ -26,9 +25,11 @@ class ResponseAdvice : ResponseBodyAdvice<Any> {
selectedConverterType: Class<out HttpMessageConverter<*>>,
request: ServerHttpRequest,
response: ServerHttpResponse
): Any? {
val result = BaseResult.success(body)
// String 类型转换为 JSON 字符串
return if (body is String) result.toJson() else result
): Any? = if (selectedContentType != MediaType.APPLICATION_JSON) {
// 非 JSON 返回不作处理
body
} else {
// JSON 返回包装返回
BaseResult.success(body)
}
}
2 changes: 1 addition & 1 deletion spring-boot-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<version>3.2.5</version>
<relativePath/>
</parent>

Expand Down

0 comments on commit 74a0c00

Please sign in to comment.