$ show LianjiaTech/retrofit-spring-boot-starter
This is a Spring Boot starter library that deeply integrates Square's Retrofit HTTP client with Spring Boot's auto-configuration, allowing developers to declare typed HTTP APIs via Java interfaces annotated with `@RetrofitClient`. The library provides a comprehensive set of features: automatic OkHttpClient management, a pluggable interceptor chain (global, network, path-match), configurable retry with exponential backoff and jitter, circuit breaking (via Sentinel or Resilience4j), logging strategies, Micrometer metrics, custom error decoding, and connection pooling. It also supports Spring Boot Actuator for runtime introspection, GraalVM native image/AOT compilation, and reactive call adapters (RxJava 2/3, Reactor). The architecture is modular with clear SPIs for extensibility (e.g., `SourceOkHttpClientRegistrar`, `GlobalInterceptor`, `RetrofitDegrade`, `ErrorDecoder`), and the configuration is annotation-driven with global defaults overridable per-client. The codebase is well-tested with integration tests using MockWebServer, and documentation is provided in multiple languages. However, the tight coupling to specific versions of Retrofit, OkHttp, and Spring Boot introduces moderate technical drift, as some optional dependencies (e.g., Sentinel 1.8.6, Resilience4j 1.7.1) are older. The library is primarily a framework integration rather than a standalone application, but its design patterns serve as a reference for building Spring Boot auto-configuration with complex SPI chains.
The LLM identified 4 files worth highlighting for reuse:
Implements a Retrofit/OkHttp interceptor that delegates HTTP error decoding to a pluggable `ErrorDecoder` SPI. On a non-2xx response, it invokes the `ErrorDecoder` to produce a domain-specific exception, and wraps the response body for error details. This is a clean application of the Strategy pattern, decoupling error handling from the HTTP client. It is self-contained and could be extracted into any OkHttp-based project to standardize error handling.
Provides an abstract base class for path-matching interceptors using Ant-style patterns. Users define custom annotations annotated with `@InterceptMark`, which specify include/exclude path patterns. Subclasses implement `doIntercept()` for the actual logic. This combines the Template Method pattern with a declarative annotation-driven approach, enabling reusable interceptors (e.g., signing, authentication) that can be applied per-method via custom annotations. The pattern is self-contained and demonstrates advanced annotation processing with Spring's `AnnotatedElementUtils`.
Implements an OkHttp interceptor that adds retry logic with configurable backoff strategies (none, linear, exponential, random exponential with jitter). It filters retries based on HTTP status codes and exception types, and supports maximum retry count and interval. The retry configuration is defined via the `@Retry` annotation with global defaults. This component is a clean, reusable retry mechanism that can be extracted into any HTTP client project, demonstrating careful handling of retry semantics and idempotency.
Implements Spring's `BeanFactoryAotProcessor` to register reflection hints for all `@RetrofitClient` interfaces, enabling GraalVM native image compilation. It scans for `@RetrofitClient` beans, records their methods, return types, and parameter types, and registers them for reflection and proxy generation. This is a non-trivial use of Spring's AOT API that demonstrates how to programmatically add runtime hints for dynamic proxy interfaces. It is self-contained and can serve as a reference for creating AOT-friendly Spring Boot starters.