Last active
December 5, 2021 19:50
-
-
Save joshlong/1f0c18179b0242c5473ca377b1d7819f to your computer and use it in GitHub Desktop.
FunctionalReactiveHttpApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.reactive | |
import org.reactivestreams.Publisher | |
import org.springframework.boot.autoconfigure.SpringBootApplication | |
import org.springframework.boot.builder.SpringApplicationBuilder | |
import org.springframework.cloud.gateway.handler.predicate.RoutePredicates.path | |
import org.springframework.cloud.gateway.route.gateway | |
import org.springframework.context.support.beans | |
import org.springframework.data.annotation.Id | |
import org.springframework.data.mongodb.core.mapping.Document | |
import org.springframework.data.mongodb.repository.ReactiveMongoRepository | |
import org.springframework.web.reactive.function.server.ServerResponse | |
import org.springframework.web.reactive.function.server.body | |
import org.springframework.web.reactive.function.server.router | |
import reactor.core.publisher.toFlux | |
@SpringBootApplication | |
class FunctionalReactiveHttpApplication | |
fun main(args: Array<String>) { | |
SpringApplicationBuilder() | |
.sources(FunctionalReactiveHttpApplication::class.java) | |
.initializers(beans { | |
bean { | |
router { | |
val customerRepository = ref<CustomerRepository>() | |
GET("/customers") { ServerResponse.ok().body(customerRepository.findAll()) } | |
GET("/customers/{id}") { ServerResponse.ok().body(customerRepository.findById(it.pathVariable("id"))) } | |
} | |
} | |
}) | |
.run(*args) | |
} | |
interface CustomerRepository : ReactiveMongoRepository<Customer, String> | |
@Document | |
data class Customer(@Id val id: String, val name: String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment