Quick revision
The short answer
I would start with an inventory, by code search and dependency analysis including libraries that use HttpURLConnection internally, and rank the findings by risk, user-facing paths without timeouts first. The target is RestClient or java.net.http.HttpClient delivered through the shared HTTP client library with standard timeouts, retries and metrics. For RestTemplate code, the low-risk first step is to switch the request factory to JdkClientHttpRequestFactory or an Apache HttpClient 5 factory, then migrate to RestClient. An ArchUnit or static analysis rule stops new direct uses, progress is tracked per service, and integration tests check behaviour differences such as redirects, proxies, HTTP/2 and header casing.
What to remember
In this answer, we planned the retirement of HttpURLConnection across 30 services. We built an inventory with code search and jdeps, ranked user-facing paths without timeouts first, and made RestClient or java.net.http.HttpClient, delivered through the shared HTTP library with standard timeouts, retries and metrics, the single target. We switched RestTemplate request factories as a low-risk first step, migrated direct uses, froze new ones with an ArchUnit rule, tracked progress per service, and tested the behaviour differences in redirects, proxies, HTTP/2, header casing and retries.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would start with an inventory, by code search and dependency analysis including libraries that use HttpURLConnection internally, and rank the findings by risk, user-facing paths without timeouts first. The target is RestClient or java.net.http.HttpClient delivered through the shared HTTP client library with standard timeouts, retries and metrics. For RestTemplate code, the low-risk first step is to switch the request factory to JdkClientHttpRequestFactory or an Apache HttpClient 5 factory, then migrate to RestClient. An ArchUnit or static analysis rule stops new direct uses, progress is tracked per service, and integration tests check behaviour differences such as redirects, proxies, HTTP/2 and header casing.
Why this question is asked
A security scan that finds the same old API in 90 places is a familiar kind of finding: nothing is broken today, yet every one of those places is a potential hung thread, and nobody owns the problem as a whole. Interviewers ask this to see whether we can turn a scattered finding into a plan with an order, a target, a guardrail and a way to show progress, without breaking 30 services in the process. In this answer, we will build the inventory and rank it, define the target, take the safe first step for RestTemplate, migrate the rest, add a guardrail against new uses, and look carefully at the behaviour differences that can break callers.
1A Quick Recap: Why HttpURLConnection Is Flagged
HttpURLConnection, the HTTP client built into the JDK since its early days, is not deprecated, but it has risky defaults: its connect and read timeouts are 0, meaning infinite, it has no overall request timeout, its connection reuse depends on every caller reading and closing streams correctly, and it only speaks HTTP/1.1. Spring's SimpleClientHttpRequestFactory, the default for new RestTemplate(), is built on it, so a RestTemplate created that way inherits the same weaknesses unless someone configured timeouts on the factory. That is why the scan counts both.
2Inventory and Rank by Risk
The first step is to find every use, including the ones that do not appear as the class name in our own code.
The jdeps step matters, because some SDKs and older client libraries use HttpURLConnection by default, and those calls are invisible to a text search of our repositories. For each finding we record the service, the call site, whether timeouts are set, and whether it sits on a user-facing path. Then we rank: user-facing paths without timeouts first, because they are the ones that can take a service down; background jobs and admin tools later. In a typical result, perhaps 15 of the 90 places are user-facing without timeouts, and those are the first sprint of work.
3Define One Target, Delivered Through the Shared Library
Migrating 90 call sites to 90 slightly different configurations would just create a new inventory problem. So the target is one supported way to make HTTP calls, provided by the shared HTTP client library, the company starter: RestClient, available since Spring Framework 6.1, for Spring services, and java.net.http.HttpClient for the few non-Spring components. The library builds the clients with standard connect and request timeouts, a pooled connection manager, retry rules for idempotent calls, and Micrometer metrics and tracing on every call. Teams then migrate to the library, not to an API, and get the right defaults by doing nothing.
4Switch the RestTemplate Request Factory First
For RestTemplate code there is a cheap and safe intermediate step: keep the RestTemplate and its call sites, and only change the request factory underneath. The application code does not change at all, but the calls stop using HttpURLConnection.
This step fixes the timeout risk for every call site of that RestTemplate at once, so it is the highest value per hour of work in the whole plan. RestClient.create(restTemplate) then lets teams move call sites to the fluent API gradually, reusing the same factory, message converters and interceptors, so both styles can coexist during the migration.
5Migrate Direct Uses and Add a Guardrail
Direct HttpURLConnection code is rewritten to use the library's RestClient or HttpClient, in the ranked order. While that happens, we must stop the number from growing. An ArchUnit rule, a unit test that checks dependencies between classes, fails the build when someone adds a new use.
- Inventory: grep plus jdeps on shipped jars
- Rank: user-facing without timeouts first
- Ship target in the shared HTTP starter
- Freeze with an ArchUnit ruleno new uses from day one
- Switch RestTemplate factoriesbiggest risk removed quickly
- Migrate direct uses and call sites to RestClient
- Integration tests for behaviour differences
- Track per service until zero
8 steps in order. Play them, or step through them one at a time.
Existing violations can be frozen with ArchUnit's FreezingArchRule, which records today's violations and fails only on new ones, so the rule can be switched on in every service on day one. A Semgrep or Error Prone rule in the shared CI pipeline does the same job for teams that do not use ArchUnit. Progress is tracked per service on a simple dashboard: remaining call sites, how many are user-facing, and whether the request factory switch is done.
6Behaviour Differences That Can Break Callers
Changing the transport is not purely mechanical; the clients behave differently in ways that some callers depend on without knowing it. These differences are why each migrated service needs integration tests against realistic stubs, for example with WireMock, rather than only unit tests.
| Behaviour | HttpURLConnection / Simple factory | JDK HttpClient / Apache 5 |
|---|---|---|
| Redirects | Followed (Simple factory: GET only) | JDK default: never; Apache: per config |
| Proxy settings | http.proxyHost system properties | JDK: ProxySelector; Apache: only if enabled |
| Protocol | HTTP/1.1 only | JDK prefers HTTP/2, may try h2c upgrade |
| Header names | Sent as written | Lower case over HTTP/2 |
| Restricted headers | Host, Content-Length settable in places | JDK refuses some, e.g. Host, Connection |
| Silent POST retry | sun.net.http.retryPost, on by default | No silent retry |
| Default timeouts | Infinite | Whatever the starter sets |
Two of these deserve special attention. Redirects: SimpleClientHttpRequestFactory follows redirects for GET requests only, while a default JDK HttpClient never follows them, so a caller that relied on a redirect to a new address suddenly gets a 301 back. Proxies: the old stack reads the http.proxyHost family of system properties, Apache HttpClient 5 ignores them unless the client is built with system properties enabled, and corporate egress through a proxy can silently stop working. HTTP/2 can also surprise: the JDK client prefers HTTP/2 and, for plain http URLs, sends an upgrade request that a few old servers mishandle, and HTTP/2 sends header names in lower case, which breaks servers that compare header names case-sensitively. Setting the version to HTTP_1_1 for such a peer is a simple fix.
7Follow-up questions
What behaviour differences could break callers?
The main ones are redirects, because the Simple factory follows them for GET and the JDK client by default does not; proxy configuration, because the old stack reads system properties that Apache HttpClient 5 ignores by default; HTTP/2 negotiation and the h2c upgrade on plain http URLs; lower-case header names over HTTP/2; headers the JDK client refuses to let us set, such as Host; and the old stack's silent retry of POST. Timeouts that suddenly exist can also break slow endpoints that used to succeed after a long time, so those need explicit, longer limits.
How long would you expect this to take?
The factory switch for RestTemplate usages can be done in a few weeks across 30 services, because it is configuration in one place per service. Rewriting direct HttpURLConnection code depends on how tangled it is; with the guardrail in place and the risky paths done first, the remaining tail can be finished as part of normal work over a quarter or two.
Why not just set sun.net.client.defaultReadTimeout everywhere and stop?
It is a fine emergency measure, because it caps the worst risk with no code changes. But it is a per-read timeout only, applies the same value to every call, and leaves the other weaknesses, so it buys time for the migration rather than replacing it.
8Conclusion
In this answer, we planned the retirement of HttpURLConnection across 30 services. We built an inventory with code search and jdeps, ranked user-facing paths without timeouts first, and made RestClient or java.net.http.HttpClient, delivered through the shared HTTP library with standard timeouts, retries and metrics, the single target. We switched RestTemplate request factories as a low-risk first step, migrated direct uses, froze new ones with an ArchUnit rule, tracked progress per service, and tested the behaviour differences in redirects, proxies, HTTP/2, header casing and retries.
Open with
I would run it as a small program rather than 90 separate fixes. First an inventory: code search for HttpURLConnection, openConnection, SimpleClientHttpRequestFactory and new RestTemplate, plus jdeps on the shipped jars to find libraries that use HttpURLConnection internally.
Then, if they want more
I would rank the results by risk, with user-facing paths without timeouts first. Second, one target, delivered through our shared HTTP client starter: RestClient for Spring services and java.net.http.HttpClient elsewhere, with standard timeouts, pooling, retry rules for idempotent calls and metrics.
Third, the quick win: for RestTemplate code, I would swap the request factory to JdkClientHttpRequestFactory or the Apache HttpClient 5 factory. The call sites do not change, but every call gets the new transport and timeouts. Then teams can move call sites to RestClient gradually with RestClient.create of the existing RestTemplate.
Fourth, a guardrail: an ArchUnit rule banning HttpURLConnection and the simple factory, frozen so existing violations are allowed but new ones fail the build. I would track progress per service on a dashboard.
And I would insist on integration tests, because behaviour changes: the simple factory follows redirects for GET while the JDK client does not by default, proxy system properties are handled differently, the JDK client prefers HTTP/2 and lower-cases header names, and the old stack silently retries POST.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A security scan finds HttpURLConnection used directly in 90 places across 30 services...": https://scrivio-interview-library.netlify.app/java/networking/9f47289875/