// Created on savesnippets.com ยท https://savesnippets.com/6Wc4k9HAE4IQXG public class HttpRequest { private final String url; private final String method; private final Map headers; private final String body; private final int timeoutMs; private HttpRequest(Builder b) { this.url = Objects.requireNonNull(b.url, "url"); this.method = b.method; this.headers = Map.copyOf(b.headers); this.body = b.body; this.timeoutMs = b.timeoutMs; } public static Builder builder() { return new Builder(); } public static class Builder { private String url; private String method = "GET"; private final Map headers = new HashMap<>(); private String body; private int timeoutMs = 30_000; public Builder url(String url) { this.url = url; return this; } public Builder method(String m) { this.method = m; return this; } public Builder header(String k, String v) { headers.put(k, v); return this; } public Builder body(String body) { this.body = body; return this; } public Builder timeoutMs(int ms) { this.timeoutMs = ms; return this; } public HttpRequest build() { return new HttpRequest(this); } } } // Usage var req = HttpRequest.builder() .url("https://api.example.com/users") .method("POST") .header("Content-Type", "application/json") .body("{\"name\":\"Alice\"}") .timeoutMs(10_000) .build();