- Kotlin Blueprints
- Ashish Belagali Hardik Trivedi Akshay Chordiya
- 180字
- 2021-07-02 21:50:18
Service/Controller
The MessageController handles all the URLs, or you can say operations, related to messages. To have a separation of concern in our code. The actual job of saving the message, that is the database operations, is delegated to the respective Repository class (MessageRepository):
/**
* Exposes the operations related to creating and showing
* messages through URLs using REST
*/
@RestController
@RequestMapping("/message")
class MessageController(val repository: MessageRepository) {
val broadcaster = ReactiveBroadcaster()
/**
* Creates new message and saves it into DB
*/
@PostMapping
@ResponseStatus(CREATED)
fun create(@RequestBody message: Message): Message {
val msg = repository.insert(message)
broadcaster.send(msg)
return msg
}
/**
* Get list of all the messages
*/
@GetMapping
fun list() = repository.findAll()
/**
* Get list of messages in the given bounds
*/
@GetMapping("/bbox/{xMin},{yMin},{xMax},{yMax}")
fun findByBoundingBox(@PathVariable xMin: Double, @PathVariable
yMin: Double,
@PathVariable xMax: Double, @PathVariable
yMax: Double)
= repository.findByBoundingBox(PGbox2d(Point(xMin, yMin),
Point(xMax, yMax)))
/**
* Subscribes to receive the updates regarding the messages
*/
@GetMapping("/subscribe")
fun subscribe()= broadcaster.subscribe()
}
@GetMapping and @PostMapping annotations are just method-specific shortcuts for @RequestMapping annotations available since Spring Framework 4.3.
推薦閱讀
- Hyper-V 2016 Best Practices
- PaaS程序設計
- Developing Middleware in Java EE 8
- R語言游戲數(shù)據(jù)分析與挖掘
- 零基礎學Java(第4版)
- Babylon.js Essentials
- JavaScript動態(tài)網(wǎng)頁編程
- Kotlin極簡教程
- Magento 2 Beginners Guide
- Node.js區(qū)塊鏈開發(fā)
- INSTANT Apache Hive Essentials How-to
- Mastering Unreal Engine 4.X
- 輕松學Scratch 3.0 少兒編程(全彩)
- C語言程序設計
- HTML5+CSS3+jQuery Mobile+Bootstrap開發(fā)APP從入門到精通(視頻教學版)