I am writing a Spring WebFlux route in Kotlin to handle POST requests with a JSON body. However, after sending my cURL
request, I can see that the ServerRequest
object has basically nothing in it, certainly not the JSON I sent.
curl -v -X POST -H "Content-Type: application/json" -d '{"key1":"value1","key2":"value2"}' http://myserver:8989/query
SearchServerConfig.kt
@Configuration
open class SearchServerConfig {
@Bean
open fun searchServer(): ISearchServer {
return SearchServer()
}
@Bean
open fun route(searchServer: ISearchServer): RouterFunction<ServerResponse?> {
return RouterFunctions.route(
RequestPredicates.POST("/query").and(RequestPredicates.accept(MediaType.APPLICATION_JSON)).and(RequestPredicates.contentType(MediaType.APPLICATION_JSON)),
HandlerFunction { request: ServerRequest? -> searchServer.performQuery(request)!!
})
}
}
SearchServer.kt
open class SearchServer() : ISearchServerService {
companion object {
val logger = LoggerFactory.getLogger(SearchServer::class.java)
}
override fun performQuery(request: ServerRequest?): Mono<ServerResponse?>? {
logger.debug("!!>> request=[$request]")
...
}
This log output from the logs only contains:
!!>> request=[HTTP POST /query]
So obviously, when I try to extract the JSON body
val bodyData: String = request?.bodyToMono(JSONObject::class.java)?.toProcessor()?.peek()!!.toString()
it throws a NPE. Why is there no body from the cURL
request present, or any of the headers, or anything really?