Skip to content

Commit 0adf4ee

Browse files
committed
add response advice for annotated response wrapping
1 parent a230c1f commit 0adf4ee

File tree

7 files changed

+74
-2
lines changed

7 files changed

+74
-2
lines changed

README.md

Whitespace-only changes.

sample/src/main/kotlin/io/hndrs/api/sample/SampleController.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package io.hndrs.api.sample
22

3+
import io.hndrs.api.response.JsonApiResponse
4+
import io.hndrs.api.response.Response
35
import org.springframework.http.HttpStatus
6+
import org.springframework.http.ResponseEntity
47
import org.springframework.web.bind.annotation.GetMapping
58
import org.springframework.web.bind.annotation.RestController
69
import org.springframework.web.server.ResponseStatusException
@@ -13,4 +16,27 @@ class SampleController {
1316
fun errors() {
1417
throw ResponseStatusException(HttpStatus.CONFLICT)
1518
}
19+
20+
@GetMapping("/address1")
21+
@JsonApiResponse
22+
fun annotated(): Address {
23+
return Address()
24+
}
25+
26+
@GetMapping("/address2")
27+
fun wrapped(): Response<Address> {
28+
return Response(Address())
29+
}
30+
31+
@GetMapping("/address3")
32+
@JsonApiResponse
33+
fun annotatedResponseEntity(): ResponseEntity<Address> {
34+
return ResponseEntity.ok(Address())
35+
}
36+
37+
data class Address(
38+
val street: String = "147 Bleecker St",
39+
val locality: String = "New York",
40+
val zip: String = "10012"
41+
)
1642
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dependencies {
2-
implementation(project(":spring-json-api"))
2+
api(project(":spring-json-api"))
33
testImplementation(group = "org.springframework.boot", name = "spring-boot-starter-test")
44
}

spring-json-api-starter/src/main/kotlin/io/hndrs/api/autoconfigure/JsonApiAutoConfiguration.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.hndrs.api.autoconfigure
22

33
import io.hndrs.api.exception.ExceptionHandler
4+
import io.hndrs.api.response.ResponseAdvise
45
import org.springframework.context.annotation.Bean
56
import org.springframework.context.annotation.Configuration
67

@@ -11,4 +12,9 @@ open class JsonApiAutoConfiguration {
1112
open fun exceptionAdvice(): ExceptionHandler {
1213
return ExceptionHandler()
1314
}
15+
16+
@Bean
17+
open fun responseAdvice(): ResponseAdvise {
18+
return ResponseAdvise()
19+
}
1420
}

spring-json-api-starter/src/test/kotlin/io/hndrs/api/autoconfigure/JsonApiAutoConfigurationTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.hndrs.api.autoconfigure
22

33
import io.hndrs.api.exception.ExceptionHandler
4+
import io.hndrs.api.response.ResponseAdvise
45
import org.junit.jupiter.api.Assertions
56
import org.junit.jupiter.api.DisplayName
67
import org.junit.jupiter.api.Test
@@ -17,6 +18,7 @@ internal class JsonApiAutoConfigurationTest {
1718
AutoConfigurations.of(JsonApiAutoConfiguration::class.java)
1819
).run {
1920
Assertions.assertNotNull(it.getBean(ExceptionHandler::class.java))
21+
Assertions.assertNotNull(it.getBean(ResponseAdvise::class.java))
2022
}
2123
}
2224
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.hndrs.api.response
2+
3+
import org.springframework.core.MethodParameter
4+
import org.springframework.http.MediaType
5+
import org.springframework.http.ResponseEntity
6+
import org.springframework.http.converter.HttpMessageConverter
7+
import org.springframework.http.server.ServerHttpRequest
8+
import org.springframework.http.server.ServerHttpResponse
9+
import org.springframework.web.bind.annotation.ControllerAdvice
10+
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice
11+
12+
@ControllerAdvice
13+
class ResponseAdvise : ResponseBodyAdvice<Any> {
14+
15+
override fun beforeBodyWrite(
16+
value: Any?,
17+
returnType: MethodParameter,
18+
selectedContentType: MediaType,
19+
selectedConverterType: Class<out HttpMessageConverter<*>>,
20+
request: ServerHttpRequest,
21+
response: ServerHttpResponse
22+
): Any? {
23+
return when (value) {
24+
is ResponseEntity<*> -> ResponseEntity.status(value.statusCode).headers(value.headers).body(value.body)
25+
null -> null
26+
else -> Response(value)
27+
}
28+
}
29+
30+
override fun supports(returnType: MethodParameter, converterType: Class<out HttpMessageConverter<*>>): Boolean {
31+
return returnType.method?.isAnnotationPresent(JsonApiResponse::class.java) ?: false
32+
}
33+
}
34+
35+
@Retention(AnnotationRetention.RUNTIME)
36+
@Target(AnnotationTarget.FUNCTION)
37+
annotation class JsonApiResponse
38+

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.1
1+
1.0.0

0 commit comments

Comments
 (0)