Skip to content

Instantly share code, notes, and snippets.

@IvanZelenskyy
Forked from anonymous/demo.kt
Last active September 2, 2017 16:48
Show Gist options
  • Save IvanZelenskyy/6f87bf535c89bed7df5bff45fa7b006f to your computer and use it in GitHub Desktop.
Save IvanZelenskyy/6f87bf535c89bed7df5bff45fa7b006f to your computer and use it in GitHub Desktop.
Try Kotlin with spring-boot 2.0.0M3
package com.example.demo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.*
@SpringBootApplication
@EnableAutoConfiguration
class DemoApplication
fun main(args: Array<String>) {
SpringApplication.run(DemoApplication::class.java, *args)
}
@RestController
class MyRestCtrl(@Autowired val service:MyService){
@GetMapping("/users") fun users() = service.get()
@GetMapping("/user/{name}") fun user(name:String) = service.get().find { it.name==name }
@DeleteMapping("/users") fun clear(name:String) = service.reset()
@PostMapping("/user") fun put(@RequestBody u:User):User {
service.add(u)
return u
}
}
data class User(val name:String, val age:Int)
@Service class MyService{
val lst: MutableList<User> = mutableListOf()
fun add(u:User) = lst.add(u)
fun reset() = lst.clear()
fun get() = lst
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment