Screenshot API Java Spring Boot Guide 2026 — WebClient, Async & S3

Integrate SnapAPI into Spring Boot with WebClient reactive calls, async processing, Spring Cache integration, and S3 storage. Full Java code examples.

Start Free — 200 screenshots/month

Spring Boot Screenshot API Quick Start

Add the SnapAPI Java SDK dependency to your Maven pom.xml or Gradle build file and configure the SnapAPI client as a Spring Bean. Define a SnapApiClient configuration class annotated with @Configuration that creates a Bean initialized with the API key loaded from Spring's application properties using @Value. Inject the client bean into your service classes using constructor injection and call the screenshot method with a SnapApiOptions object configured with the target URL, format, and viewport dimensions. The Java SDK returns a SnapApiResult object containing the image bytes and response metadata, or throws a SnapApiException with a typed error code if the request fails.

@Configuration
public class SnapApiConfig {
    @Bean
    public SnapApiClient snapApiClient(
            @Value("${snapapi.key}") String apiKey) {
        return new SnapApiClient(apiKey);
    }
}

@Service
public class ScreenshotService {
    private final SnapApiClient client;

    public ScreenshotService(SnapApiClient client) {
        this.client = client;
    }

    public byte[] captureScreenshot(String url) {
        SnapApiOptions opts = SnapApiOptions.builder()
            .url(url)
            .format("png")
            .fullPage(true)
            .width(1280)
            .build();
        return client.screenshot(opts).getImage();
    }
}

Spring Boot WebClient Reactive Screenshot API

Spring WebFlux applications use WebClient to make non-blocking HTTP calls to the SnapAPI REST endpoint, enabling reactive screenshot processing that does not block the event loop. Configure a WebClient bean with the SnapAPI base URL and a default Authorization header populated from the API key application property. Define a reactive screenshot method that returns Mono of byte array, making the SnapAPI HTTP call asynchronously and propagating errors as typed exceptions using onErrorMap. Compose multiple concurrent screenshot captures using Flux.fromIterable over a list of URLs and flatMap with a concurrency limit that controls how many simultaneous SnapAPI calls the reactive pipeline maintains, preventing rate limit exhaustion while maximizing throughput within the plan's allowed concurrency.

Spring Cache Integration for Screenshot Results

Spring Boot applications use the Spring Cache abstraction to cache SnapAPI screenshot results transparently with a single @Cacheable annotation on the screenshot service method. Configure a CacheManager bean backed by Redis using Spring Data Redis, enabling screenshot cache entries to persist across application restarts and be shared across all instances of the service in a horizontally scaled deployment. Annotate the screenshot service method with @Cacheable specifying the cache name and a key expression derived from the URL and screenshot options, so that repeated calls with identical parameters return the cached byte array without hitting SnapAPI. Use @CacheEvict on a separate method that triggers cache invalidation for a specific URL when the page content is known to have changed, enabling explicit cache refresh for URLs where the cached screenshot is known to be stale.

Async Screenshot Processing with Spring @Async

Spring Boot REST APIs that need to accept screenshot requests without blocking the HTTP response thread use the @Async annotation to offload SnapAPI calls to a dedicated thread pool. Define an AsyncConfiguration class that configures a ThreadPoolTaskExecutor with a core pool size matching your SnapAPI plan's concurrency limit, a maximum pool size to absorb burst traffic, and a bounded queue to prevent unbounded task accumulation during traffic spikes. Annotate the screenshot service method with @Async to execute it on the configured thread pool and return a CompletableFuture that the controller can return as a DeferredResult, freeing the HTTP request thread while the SnapAPI call completes asynchronously. Chain CompletableFuture.thenApply and thenCompose callbacks to process the screenshot result, store it in S3, and return a JSON response to the client with the screenshot URL after the async processing completes.

Spring Boot Screenshot API with S3 Storage

Spring Boot applications store SnapAPI screenshot results in AWS S3 using the AWS SDK for Java v2 with its asynchronous S3AsyncClient, enabling non-blocking upload of screenshot bytes to S3 without consuming a thread during the upload operation. Inject an S3AsyncClient bean configured with the target bucket name and region from application properties, then compose the SnapAPI screenshot call with a subsequent S3 put-object call using CompletableFuture chaining. Generate the S3 object key from a hash of the target URL and capture timestamp, ensuring unique keys for each capture while enabling lookup of existing captures by URL. Configure S3 bucket lifecycle rules to transition screenshots to S3 Glacier Instant Retrieval after thirty days and expire objects older than one year, managing storage costs for applications that capture screenshots at high volume over extended time periods.

Spring Boot Screenshot API Rate Limiting with Resilience4j

Spring Boot applications protecting SnapAPI calls from rate limit exhaustion use Resilience4j's RateLimiter component to enforce a request rate that stays within the SnapAPI plan's allowed rate. Configure a RateLimiter bean with a limit-for-period matching the plan's per-second request rate and a limit-refresh-period of one second, then wrap each SnapAPI client call with the RateLimiter.executeCallable method to acquire a rate limiter permit before making the HTTP call. When the rate limiter is at capacity, executeCallable throws a RequestNotPermitted exception that your application can handle by queuing the screenshot request for later processing or returning a 429 response to the client. Combine Resilience4j's RateLimiter with its Retry component to automatically retry SnapAPI calls that return 429 responses due to plan-level rate limiting, using an exponential backoff retry configuration that respects the Retry-After header value returned by SnapAPI in rate limit responses.

Spring Boot Screenshot API with Kafka Integration

Large-scale Spring Boot applications processing screenshot jobs at high volume use Apache Kafka as the job queue, decoupling screenshot request producers from the SnapAPI-calling consumers and providing durable job storage that survives application restarts. Define a Kafka topic for screenshot jobs with a partition count that supports the desired consumer parallelism, and configure consumer groups with a concurrency level that matches your SnapAPI plan's rate limit. Spring Kafka's @KafkaListener annotation on the screenshot consumer service method handles message deserialization, thread pool management, and offset commit automatically, simplifying the consumer implementation to the core screenshot capture logic. Use Kafka's dead letter topic pattern to route messages that fail after the maximum retry count to a separate topic for manual review, preventing failed screenshot jobs from blocking the main consumer partition while preserving the failed job data for debugging and reprocessing.

Spring Boot Screenshot API Microservice Design

Organizations building screenshot capabilities as an internal microservice expose a Spring Boot REST API that wraps SnapAPI, adding authentication, rate limiting per client, usage tracking, and result caching for internal consumers. The screenshot microservice authenticates internal clients using Spring Security with JWT tokens or API keys, recording each screenshot request with the client identifier, timestamp, and target URL in a PostgreSQL database for usage attribution and billing chargeback reporting. Expose a health endpoint at /actuator/health that Spring Boot Actuator populates with the microservice's operational status including SnapAPI connectivity checks, enabling Kubernetes readiness and liveness probes to correctly manage the microservice's lifecycle based on its ability to reach SnapAPI. Deploy the screenshot microservice to Kubernetes with a HorizontalPodAutoscaler configured to scale based on the screenshot job queue depth metric published to Prometheus, automatically adding pods during high-volume screenshot processing periods and scaling down during low-traffic periods to minimize infrastructure costs.

Spring Boot Screenshot API Testing with MockMvc and WireMock

Spring Boot integration tests for screenshot service endpoints use WireMock to stub the SnapAPI HTTP endpoint, enabling tests to verify the full Spring MVC request-response cycle including authentication, parameter validation, caching behavior, and error handling without making real API calls. Configure a WireMock server in a @SpringBootTest test class with a stub that returns a test PNG fixture for valid screenshot requests and a 429 response for rate-limit simulation requests. Use MockMvc to call your screenshot controller endpoint and assert that the response status, Content-Type header, and response body match expected values for each test scenario. Test the Spring Cache integration by calling the screenshot endpoint twice with the same URL and verifying that WireMock received only one request, confirming that the second call was served from the cache without calling SnapAPI again. This testing approach provides high-confidence coverage of the screenshot integration path while keeping test execution fast and independent of external API availability.

Deploying the Spring Boot Screenshot Service

Deploy the Spring Boot screenshot microservice as a Docker container using the official Eclipse Temurin JRE base image, producing a minimal production image that includes only the JRE and the application JAR. Configure the container with the SNAPAPI_KEY environment variable injected from a Kubernetes Secret, ensuring the API key is never embedded in the container image or the deployment manifest. Set JVM memory flags with -XX:MaxRAMPercentage to allocate a fixed fraction of the container memory limit to the JVM heap, preventing out-of-memory kills when the container memory limit is set tightly. Define a Kubernetes Deployment with resource requests and limits calibrated to the observed memory and CPU usage of the screenshot service under typical load, and configure the HorizontalPodAutoscaler to maintain the screenshot service at target CPU utilization, scaling pods up during batch processing periods and back down during idle intervals to minimize infrastructure cost.