emptygif
语言: 简体中文

所有语言和框架

Spring Boot Java

一个用于 Spring Boot 的 Java 代码片段,以空 GIF 响应对 /pixel.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 示例