← Back to blog

Google Search API in Java: Complete Tutorial (2026)

Call a Google Search API from Java — a runnable java.net.http example, JSON records, curl test. 100 free searches, pay-as-you-go from $0.75/1k.

1 min read

Query Google from Java with nothing but the standard library, using serp.cheap — or drop in the official Java SDK (Spring Boot integration included). Either way it’s a plain HTTP+JSON endpoint.

Quick sanity check with curl first:

curl -X POST https://api.serp.cheap/v1/search \
  -H "X-API-Key: $SERP_CHEAP_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"q":"best electric cars 2026","gl":"us"}'

The complete example

One file, runnable on Java 17+ with java.net.http for the request and Jackson for JSON (com.fasterxml.jackson.core:jackson-databind). Set SERP_CHEAP_API_KEY first.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;

public class SerpSearch {
    static final ObjectMapper M = new ObjectMapper();

    @JsonIgnoreProperties(ignoreUnknown = true)
    record Organic(int position, String title, String link, String snippet) {}

    @JsonIgnoreProperties(ignoreUnknown = true)
    record SearchResponse(List<Organic> organic) {}

    public static void main(String[] args) throws Exception {
        String key = System.getenv("SERP_CHEAP_API_KEY");
        String body = M.writeValueAsString(new java.util.HashMap<>() {{
            put("q", "best electric cars 2026");
            put("gl", "us");
        }});

        HttpRequest req = HttpRequest.newBuilder()
            .uri(URI.create("https://api.serp.cheap/v1/search"))
            .header("X-API-Key", key)
            .header("Content-Type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(body))
            .build();

        HttpResponse<String> res = HttpClient.newHttpClient()
            .send(req, HttpResponse.BodyHandlers.ofString());

        SearchResponse data = M.readValue(res.body(), SearchResponse.class);
        for (Organic r : data.organic()) {
            System.out.printf("%2d. %s%n    %s%n", r.position(), r.title(), r.link());
        }
    }
}

records map the JSON cleanly, and @JsonIgnoreProperties(ignoreUnknown = true) means new response fields never break your build.

Spring Boot

In a Spring app, wrap the same call in a @Service using RestClient (Spring 6.1+):

@Service
public class SerpClient {
    private final RestClient http = RestClient.builder()
        .baseUrl("https://api.serp.cheap")
        .defaultHeader("X-API-Key", System.getenv("SERP_CHEAP_API_KEY"))
        .build();

    public SearchResponse search(String q, String gl) {
        return http.post().uri("/v1/search")
            .body(java.util.Map.of("q", q, "gl", gl))
            .retrieve()
            .body(SearchResponse.class);
    }
}

The official Java SDK ships this wiring — plus retries, pagination and typed parameters — as a Spring Boot starter, so you inject SerpClient and go.

Parameters worth knowing

Next

Try serp.cheap

Copy the file above, add your key, and you have live Google results in Java. 100 free searches on signup — no card required; pay-as-you-go from $0.75/1k after that, and credits stay valid for 12 months.

Frequently asked questions

How do I call a Google Search API in Java?+

Use the built-in java.net.http.HttpClient to POST a JSON body to the SERP API and map the response. With serp.cheap the endpoint is https://api.serp.cheap/v1/search with an X-API-Key header — no external HTTP library required.

Do I need a third-party Java SDK?+

No — java.net.http (Java 11+) plus any JSON library (Jackson, Gson) is enough, since a SERP API is a plain HTTP+JSON endpoint. serp.cheap also ships an official Java SDK with a Spring Boot integration if you want typed calls, retries and pagination out of the box.

Get your API key

Cheapest direct, pay by card or crypto, no KYC. Or use a marketplace.