emptygif
Язык: Русский

Все языки и фреймворки

Spring WebFlux (Kotlin coRouter) Kotlin

Пример на Kotlin для Spring WebFlux (Kotlin coRouter), который отвечает на запросы к /pixel.gif пустым GIF, декодированным один раз при запуске.

Spring WebFlux (Kotlin coRouter) Kotlin

Установка
implementation("org.springframework.boot:spring-boot-starter-webflux"); implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
Файл
PixelApplication.kt
Запуск
./gradlew bootRun
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)

Ещё на Kotlin