Created
September 2, 2017 16:44
-
-
Save anonymous/86a802a24c85075e8ba83da6f10fc677 to your computer and use it in GitHub Desktop.
Try Kotlin with spring-boot 2.0.0M3
This file contains hidden or 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.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