emptygif
언어: 한국어

모든 언어 및 프레임워크

Spring Boot Java

/pixel.gif 요청에 빈 GIF로 응답하는 Spring Boot용 Java 스니펫입니다. 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의 다른 예제