Skip to content

Instantly share code, notes, and snippets.

@joshlong
Last active December 5, 2021 19:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshlong/1f0c18179b0242c5473ca377b1d7819f to your computer and use it in GitHub Desktop.
Save joshlong/1f0c18179b0242c5473ca377b1d7819f to your computer and use it in GitHub Desktop.
FunctionalReactiveHttpApplication.kt
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