emptygif
Bahasa: Bahasa Indonesia

Semua bahasa dan framework

Spring WebFlux (Kotlin coRouter) Kotlin

Snippet Kotlin untuk Spring WebFlux (Kotlin coRouter) yang menjawab request ke /pixel.gif dengan empty GIF, yang di-decode sekali saat startup.

Spring WebFlux (Kotlin coRouter) Kotlin

Instal
implementation("org.springframework.boot:spring-boot-starter-webflux"); implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
File
PixelApplication.kt
Jalankan
./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)
}

Kami telah menjalankan snippet ini dan membandingkan byte yang dikembalikannya.

Periksa

curl -sI http://localhost:8080/pixel.gif

Situs resmi: Spring WebFlux (Kotlin coRouter)

Lainnya dalam Kotlin