-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add response advice for annotated response wrapping
- Loading branch information
1 parent
a230c1f
commit 0adf4ee
Showing
7 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
spring-json-api/src/main/kotlin/io/hndrs/api/response/ResponseAdvise.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.0.1 | ||
1.0.0 |