emptygif
Ngôn ngữ: Tiếng Việt

Tất cả ngôn ngữ và framework

Spring WebFlux (Kotlin coRouter) Kotlin

Đoạn mã Kotlin cho Spring WebFlux (Kotlin coRouter), trả lời các yêu cầu tới /pixel.gif bằng GIF rỗng, được giải mã một lần khi khởi động.

Spring WebFlux (Kotlin coRouter) Kotlin

Cài đặt
implementation("org.springframework.boot:spring-boot-starter-webflux"); implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
Tệp
PixelApplication.kt
Chạy
./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)
}

Chúng tôi đã chạy đoạn mã này và so sánh các byte nó trả về.

Kiểm tra

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

Trang chính thức: Spring WebFlux (Kotlin coRouter)

Thêm với Kotlin