emptygif
زبان: فارسی

همه زبان‌ها و فریم‌ورک‌ها

Spring Boot Java

قطعه‌کد Java برای Spring Boot که به درخواست‌های /pixel.gif با GIF خالی پاسخ می‌دهد؛ GIF فقط یک‌بار هنگام راه‌اندازی دیکود می‌شود.

Spring Boot Java

نصب
https://start.spring.io/ (dependency: Spring Web)
فایل
PixelApplication.java
اجرا
./gradlew bootRun
package com.example.pixel;

import java.util.Base64;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class PixelApplication {

    private static final byte[] GIF = Base64.getDecoder().decode("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");

    @GetMapping("/pixel.gif")
    ResponseEntity<byte[]> pixel() {
        return ResponseEntity.ok()
                .contentType(MediaType.IMAGE_GIF)
                .cacheControl(CacheControl.noStore())
                .body(GIF);
    }

    public static void main(String[] args) {
        SpringApplication.run(PixelApplication.class, args);
    }
}

این قطعه‌کد را اجرا کردیم و بایت‌هایی را که برگرداند مقایسه کردیم.

بررسی

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

سایت رسمی: Spring Boot

موارد بیشتر در Java