Quick revision
The short answer
We declare interface UserApi with @GET("users/{id}") Call<User> get(@Path("id") long id), build an OkHttpClient with connect and read timeouts, and build Retrofit with a base URL ending in a slash, that client and JacksonConverterFactory.create(mapper). Then r.create(UserApi.class) gives the implementation, and api.get(1).execute() makes a synchronous call that returns a Response<User>. Retrofit does not throw on 404 or 500, so we check resp.isSuccessful() and read errorBody() otherwise.
What to remember
In this answer, we wrote a Retrofit interface with @GET("users/{id}") and @Path, built an OkHttpClient with connect, read and call timeouts, and built Retrofit with a base URL ending in a slash, that client and the Jackson converter. We made a synchronous call with execute(), saw that network failures throw IOException while 404 and 500 return a normal Response that we must check with isSuccessful() and read through errorBody(), and saw that relative annotation paths keep the base URL's path while a leading slash drops it.
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.
We declare interface UserApi with @GET("users/{id}") Call<User> get(@Path("id") long id), build an OkHttpClient with connect and read timeouts, and build Retrofit with a base URL ending in a slash, that client and JacksonConverterFactory.create(mapper). Then r.create(UserApi.class) gives the implementation, and api.get(1).execute() makes a synchronous call that returns a Response<User>. Retrofit does not throw on 404 or 500, so we check resp.isSuccessful() and read errorBody() otherwise.
Why this question is asked
Retrofit, from Square, is one of the most widely used declarative HTTP clients on the JVM, on servers and especially on Android. Its API is small, but a few details trip people up every time: how errors are reported, why the base URL needs a trailing slash, and how relative paths resolve. Interviewers ask for it in code to check that we have used it beyond the README. In this answer, we will recap how Retrofit is put together, write the interface, build the client with timeouts and Jackson, make a synchronous call, handle errors properly, and look closely at the base URL rules.
1A Quick Recap of Retrofit and OkHttp
Retrofit turns a Java interface into an HTTP client. We annotate methods with the HTTP method and path, and Retrofit generates an implementation at runtime. It does not do the networking itself: it delegates to OkHttp, also from Square, which handles connections, pooling, TLS and timeouts. And it does not know JSON by itself either: a converter, such as the Jackson converter from the converter-jackson artifact, turns response bodies into objects and objects into request bodies.
So there are three pieces to configure: the interface, the OkHttpClient for the transport, and the Retrofit instance that ties them together with a base URL and a converter.
2Writing the Interface
The interface describes one endpoint. The method returns Call<User>, Retrofit's object representing one request that has not been sent yet.
Notice that the path has no leading slash. It is relative, so it is appended to the base URL's path. @Path("id") fills the {id} placeholder, and Retrofit URL-encodes the value. A Java record works with Jackson 2.12 or later.
3Building the Client With Timeouts and Jackson
OkHttp's builder takes the timeouts. The connect timeout limits establishing the connection, the read timeout limits the wait between bytes of the response, and a call timeout, if we want one, limits the whole call from start to finish.
We create the OkHttpClient, the Retrofit instance and the UserApi once and reuse them for the whole application. Each OkHttpClient owns a connection pool and threads, so creating one per call wastes sockets and threads. The API object is thread-safe.
4Making a Synchronous Call
execute() sends the request on the current thread and blocks until the response arrives. It returns Response<User>, which wraps the status, headers and either the converted body or the raw error body.
- execute()
- Network error or timeout?YesIOException is thrownNoA Response is returned
- resp.isSuccessful()?Yesresp.body() holds the UserNoread and close resp.errorBody()
3 steps in order. Play them, or step through them one at a time.
HTTP errors arrive as a normal Response, not as an exception.
Notice the two separate failure paths. Network problems, such as a refused connection or a timeout, throw IOException from execute(). HTTP error statuses do not throw at all: execute() returns normally with isSuccessful() false, and resp.body() is null. This surprises developers used to Spring's RestClient, which throws HttpClientErrorException for a 404. If we forget the check, we get a NullPointerException later, far from the real cause. We also close the error body, because it holds the connection until it is read or closed.
For asynchronous use, enqueue(callback) runs the call on OkHttp's dispatcher threads, and a Call can only be executed once; to retry, we use call.clone().
5How the Base URL and Paths Combine
Retrofit resolves the annotation path against the base URL the same way a browser resolves a link against a page, which leads to two rules. First, the base URL must end with a slash; Retrofit rejects one without it at build time with an IllegalArgumentException saying the base URL must end in a slash. Second, a path with a leading slash is absolute: it replaces the whole path of the base URL, keeping only the host.
So when a base URL has a path, such as a version prefix, relative paths without a leading slash are what we almost always want. This is one of the most common Retrofit bugs, because both versions work when the base URL has no path.
6Follow-up questions
How do you turn error responses into exceptions?
Either check isSuccessful() at each call site, or wrap the API in a small class that throws a domain exception such as UserNotFoundException on 404. A custom CallAdapter.Factory can do it for every method, so methods can return User directly and throw on errors, which gives behaviour closer to Spring's clients.
Can the interface method return User directly instead of Call<User>?
Retrofit 2.6 and later support Kotlin suspend functions returning the body directly, and other call adapters support RxJava and other types. For plain Java, Call<T> is the default; returning T directly needs a custom call adapter. Check the call adapters available for your Retrofit version.
How would you add an Authorization header to every request?
With an OkHttp interceptor added to the OkHttpClient builder, which copies each request with the header added. That keeps credentials out of the interface and applies them in one place. For a header that varies per call, a @Header parameter on the method works.
7Conclusion
In this answer, we wrote a Retrofit interface with @GET("users/{id}") and @Path, built an OkHttpClient with connect, read and call timeouts, and built Retrofit with a base URL ending in a slash, that client and the Jackson converter. We made a synchronous call with execute(), saw that network failures throw IOException while 404 and 500 return a normal Response that we must check with isSuccessful() and read through errorBody(), and saw that relative annotation paths keep the base URL's path while a leading slash drops it.
Open with
I would write interface UserApi with a method annotated @GET("users/{id}") returning Call<User>, taking @Path("id") long id. Then I build one OkHttpClient with connectTimeout of 2 seconds and readTimeout of 5, and maybe a callTimeout for the whole call.
Then, if they want more
The Retrofit builder gets baseUrl("https://api.example.com/"), client(ok) and addConverterFactory(JacksonConverterFactory.create(mapper)), with a mapper that ignores unknown properties. r.create(UserApi.class) gives me the implementation, and I keep all of these as singletons, because each OkHttpClient has its own pool and threads. For a synchronous call I do api.get(1).execute(), which returns Response<User>.
The important detail is error handling: execute throws IOException for network problems and timeouts, but a 404 or 500 does not throw; it returns a response with isSuccessful() false and a null body, so I check isSuccessful and otherwise read and close errorBody().
And the base URL must end with a slash, otherwise Retrofit throws at build time, and paths in the annotations should be relative, without a leading slash, because a leading slash replaces the base URL's path, so a /v2/ prefix would silently be dropped. A Call can only run once, so for a retry I would clone it.
About this answer
- Last updated
- 12 November 2026
- 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
- Spring Framework reference, Spring Web MVC
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 "Write a Retrofit interface for GET /users/{id} returning a User, build the client with a...": https://scrivio-interview-library.netlify.app/spring-boot/http-clients/42d81fb288/