Spring WebFlux (Kotlin coRouter) Kotlin
一个用于 Spring WebFlux (Kotlin coRouter) 的 Kotlin 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。
Spring WebFlux (Kotlin coRouter) Kotlin
package com.example.pixel
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import org.springframework.http.CacheControl
import org.springframework.http.MediaType
import org.springframework.web.reactive.function.server.bodyValueAndAwait
import org.springframework.web.reactive.function.server.coRouter
import java.util.Base64
private val GIF: ByteArray = Base64.getDecoder().decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==")
@SpringBootApplication
class PixelApplication {
@Bean
fun pixelRoutes() = coRouter {
GET("/pixel.gif") {
ok().contentType(MediaType.IMAGE_GIF)
.contentLength(GIF.size.toLong())
.cacheControl(CacheControl.noStore())
.bodyValueAndAwait(GIF)
}
}
}
fun main(args: Array<String>) {
runApplication<PixelApplication>(*args)
}
我们运行了此代码片段,并比对了它返回的字节。
验证
curl -sI http://localhost:8080/pixel.gif
官方网站: Spring WebFlux (Kotlin coRouter)