Skip to content

Commit

Permalink
add response advice for annotated response wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinSchramm committed Feb 7, 2021
1 parent a230c1f commit 0adf4ee
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
Empty file added README.md
Empty file.
26 changes: 26 additions & 0 deletions sample/src/main/kotlin/io/hndrs/api/sample/SampleController.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.hndrs.api.sample

import io.hndrs.api.response.JsonApiResponse
import io.hndrs.api.response.Response
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ResponseStatusException
Expand All @@ -13,4 +16,27 @@ class SampleController {
fun errors() {
throw ResponseStatusException(HttpStatus.CONFLICT)
}

@GetMapping("/address1")
@JsonApiResponse
fun annotated(): Address {
return Address()
}

@GetMapping("/address2")
fun wrapped(): Response<Address> {
return Response(Address())
}

@GetMapping("/address3")
@JsonApiResponse
fun annotatedResponseEntity(): ResponseEntity<Address> {
return ResponseEntity.ok(Address())
}

data class Address(
val street: String = "147 Bleecker St",
val locality: String = "New York",
val zip: String = "10012"
)
}
2 changes: 1 addition & 1 deletion spring-json-api-starter/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dependencies {
implementation(project(":spring-json-api"))
api(project(":spring-json-api"))
testImplementation(group = "org.springframework.boot", name = "spring-boot-starter-test")
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.hndrs.api.autoconfigure

import io.hndrs.api.exception.ExceptionHandler
import io.hndrs.api.response.ResponseAdvise
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

Expand All @@ -11,4 +12,9 @@ open class JsonApiAutoConfiguration {
open fun exceptionAdvice(): ExceptionHandler {
return ExceptionHandler()
}

@Bean
open fun responseAdvice(): ResponseAdvise {
return ResponseAdvise()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.hndrs.api.autoconfigure

import io.hndrs.api.exception.ExceptionHandler
import io.hndrs.api.response.ResponseAdvise
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
Expand All @@ -17,6 +18,7 @@ internal class JsonApiAutoConfigurationTest {
AutoConfigurations.of(JsonApiAutoConfiguration::class.java)
).run {
Assertions.assertNotNull(it.getBean(ExceptionHandler::class.java))
Assertions.assertNotNull(it.getBean(ResponseAdvise::class.java))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.hndrs.api.response

import org.springframework.core.MethodParameter
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.http.converter.HttpMessageConverter
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

@ControllerAdvice
class ResponseAdvise : ResponseBodyAdvice<Any> {

override fun beforeBodyWrite(
value: Any?,
returnType: MethodParameter,
selectedContentType: MediaType,
selectedConverterType: Class<out HttpMessageConverter<*>>,
request: ServerHttpRequest,
response: ServerHttpResponse
): Any? {
return when (value) {
is ResponseEntity<*> -> ResponseEntity.status(value.statusCode).headers(value.headers).body(value.body)
null -> null
else -> Response(value)
}
}

override fun supports(returnType: MethodParameter, converterType: Class<out HttpMessageConverter<*>>): Boolean {
return returnType.method?.isAnnotationPresent(JsonApiResponse::class.java) ?: false
}
}

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION)
annotation class JsonApiResponse

2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.1
1.0.0

0 comments on commit 0adf4ee

Please sign in to comment.