Skip to main content

Spring Boot

MediatorK integrates with Spring Boot without any special plugin — just register handlers as beans and create the mediator in a @Configuration class.

See the full Spring Boot example for a complete WebFlux CRUD API.


Setup

For the MediatorK dependency see Installation. For Spring Boot itself, follow the official Spring Initializr setup — select Kotlin, Gradle, and the Spring WebFlux starter.


Pattern

1. Handler beans

@Service
class GetUserHandler(private val repo: UserRepository) : RequestHandler<GetUserQuery, User> {
override suspend fun handle(mediator: Mediator, ctx: RequestContext, request: GetUserQuery) =
repo.findById(request.id) ?: error("Not found")
}

2. Registrar bean

Group all handlers into a MediatorRegistrar:

@Component
class UserRegistrar(
private val getUser: GetUserHandler,
private val createUser: CreateUserHandler,
) : MediatorRegistrar {
override fun register(registry: HandlerRegistry) {
registry.scope {
+getUser
+createUser
}
}
}

3. Mediator bean

Spring collects all MediatorRegistrar beans automatically:

@Configuration
class MediatorConfig(private val registrars: List<MediatorRegistrar>) {
@Bean
fun mediator(): Mediator = MediatorFactory.create(
registrars = registrars,
pipelineBehaviors = listOf(LoggingBehavior()),
)
}

4. Controller

@RestController
@RequestMapping("/users")
class UserController(private val mediator: Mediator) {

@GetMapping("/{id}")
suspend fun get(@PathVariable id: String) = mediator.send(GetUserQuery(id))

@PostMapping
suspend fun create(@RequestBody body: CreateUserRequest) =
mediator.send(CreateUserCommand(body.name, body.email))
}

Sample — Prayer Times API

The /samples/sample-spring module is a runnable Spring Boot WebFlux application that fetches prayer times and Islamic calendar months from the Aladhan API. It mirrors the Android sample structure in three layers:

LayerRoutesPattern
Before/before/prayer-times/{city} · /before/islamic-monthsController → UseCase → Repository → DataSource
After/after/prayer-times/{city} · /after/islamic-monthsController → Mediator → Handler
After Super/aftersuper/prayer-times/{city} · /aftersuper/islamic-monthsAfter + all pipeline behaviors
# Run (from the repo root)
./gradlew :samples:sample-spring:bootRun

# Try it
curl http://localhost:8081/before/prayer-times/London
curl http://localhost:8081/after/prayer-times/London
curl http://localhost:8081/aftersuper/prayer-times/London

Next

Koin