Registering Clients
A @CoApi interface becomes a bean only once a registrar finds it. There are four ways to make sure that happens. You can combine them.
| Way | Needs Spring Boot | Use it for |
|---|---|---|
| Classpath scanning (default) | yes | Clients in your application's packages |
coapi.base-packages | yes | Clients in other packages or JARs, by package |
@EnableCoApi(clients = [...]) | no | Clients listed explicitly, in any package |
CoApiDefinition beans | yes | Interfaces you cannot annotate, or definitions built in code |
Classpath scanning
With the starter on the classpath, CoApiAutoConfiguration scans the auto-configuration packages (the package of your @SpringBootApplication class and everything below it) for interfaces annotated with @CoApi. Classes are ignored.
Scanning is on unless coapi.enabled=false.
coapi.base-packages
Adds packages to scan, typically for clients shipped in a library JAR:
coapi:
base-packages:
- com.example.order.client
- com.example.payment.clientA comma-separated string (coapi.base-packages=com.a,com.b) works too.
@EnableCoApi
Registers exactly the listed interfaces:
@EnableCoApi(clients = [TodoClient::class, GitHubApiClient::class])
@SpringBootApplication
class ConsumerApplication@EnableCoApi lives in coapi-spring and works without Spring Boot. It does not depend on coapi.enabled.
Registering the same interface by more than one way is harmless: the first registration wins, and later ones are skipped with a WARN log line. The same skip applies to two different interfaces that share a name across registration paths, so give clients unique names.
CoApiDefinition beans
When you cannot put @CoApi on an interface (it comes from a third party, or the definition is computed), declare a CoApiDefinition bean instead:
@Configuration
class OrderApiConfiguration {
companion object {
@JvmStatic
@Bean
fun orderApiDefinition(): CoApiDefinition = CoApiDefinition(
name = "OrderApi",
apiType = OrderApi::class.java,
baseUrl = "lb://\${order.service-id}",
loadBalanced = false, // lb:// already enables load balancing
)
}
}Rules:
- The
@Beanmethod must be static (Kotlin:@JvmStaticin acompanion object). CoApi reads these beans before bean post-processing. A non-static method would create its configuration class too early, so startup fails instead. - Placeholders and
lb://inbaseUrlare resolved just like on the annotation. - Names must be unique. A clash with a scanned client fails startup; a clash with an
@EnableCoApiclient is skipped with aWARNlog. - Only Spring Boot auto-configuration collects these beans.
@EnableCoApialone does not.
Without Spring Boot
Use coapi-spring and @EnableCoApi on a configuration class, and provide what Boot would otherwise provide:
@Configuration
@EnableCoApi(clients = [GitHubApiClient::class])
class ClientConfiguration {
@Bean
@Scope("prototype")
fun restClientBuilder(): RestClient.Builder = RestClient.builder()
}- A
WebClient.BuilderorRestClient.Builderbean for the client mode. Make it prototype-scoped, because CoApi configures the builder it receives. coapi.modeis read from theEnvironment. Set it explicitly, becauseAUTOmay pick a mode whose builder you did not define.- Optionally,
ClientProperties/ReactiveClientProperties/SyncClientPropertiesbeans for per-client overrides. See Configuration.