Spring / Spring7 Intermediate to Advanced Interview questions
Explain the lifecycle of a request handled through an HTTP Interface client (@HttpExchange) in Spring 7?
An HTTP Interface client is a plain interface annotated with @HttpExchange-family annotations; at startup, HttpServiceProxyFactory generates a JDK dynamic proxy implementing that interface, so the application never writes an implementation by hand.
sequenceDiagram
participant App as Application code
participant Proxy as Generated proxy
participant Adapter as RestClientAdapter/WebClientAdapter
participant Client as RestClient/WebClient
App->>Proxy: call interface method
Proxy->>Proxy: build HttpRequestValues from args/annotations
Proxy->>Adapter: delegate exchange
Adapter->>Client: perform HTTP call
Client-->>Adapter: HTTP response
Adapter-->>Proxy: raw response
Proxy-->>App: converted return value
When application code calls an interface method, the proxy's invocation handler builds an HttpRequestValues object from the method's arguments and annotations - path variables, query params, headers, request body - and, if the method declares a version attribute, attaches that to the request through the configured API-versioning strategy. It then delegates to an adapter (RestClientAdapter wrapping a RestClient for synchronous use, or WebClientAdapter wrapping a WebClient for reactive use), which performs the actual HTTP exchange. The adapter's underlying client applies whatever interceptors, error handling, and observation/tracing instrumentation it's configured with, receives the response, and the proxy converts the response body into the method's declared return type via message converters before handing it back - synchronously as a plain object if backed by RestClient, or wrapped in a Mono/Flux if the interface method itself is declared reactively.
More Related questions...