Skip to content

Instantly share code, notes, and snippets.

@fredlahde
Last active August 5, 2017 20:17
Show Gist options
  • Save fredlahde/2567c91fdec1a322aa403225cd53e349 to your computer and use it in GitHub Desktop.
Save fredlahde/2567c91fdec1a322aa403225cd53e349 to your computer and use it in GitHub Desktop.
package com.example.demo
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.annotation.Bean
import org.springframework.data.annotation.Id
import org.springframework.data.repository.reactive.ReactiveCrudRepository
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.server.ServerRequest
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.Mono
import java.math.BigInteger
data class User(
val name: String = "",
@Id val id: BigInteger? = null
)
interface UserRepository : ReactiveCrudRepository<User, BigInteger>
@SpringBootApplication
class DemoApplication(val userController: UserController) {
@Bean fun apiRoutes() = router {
path("/users").nest {
GET("/", userController::index)
GET("/{id}", userController::show)
POST("/", userController::store)
}
}
}
@Component class UserController(val repo: UserRepository) {
fun index(req: ServerRequest) = ServerResponse.ok().body(repo.findAll())
fun show(req: ServerRequest) = Mono.justOrEmpty(req.pathVariable("id"))
.map(::BigInteger)
.flatMap { ServerResponse.ok().body(repo.findById(it)) }
fun store(req: ServerRequest) = ServerResponse.ok().body(
repo.saveAll(req.bodyToFlux(User::class.java))
)
}
fun main(args: Array<String>) {
SpringApplication.run(DemoApplication::class.java, *args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment