Kotlin 1.1.61 + Spring で Web Application を作る (2 of 4) の続きです。
Build Web API
HTMLで表示するViewは作りましたから、次は API を作ります。 一覧が取得できる簡単な API を考えます。
Return value of the controller method can be Kotlin Map, which will be converted well to JSON. To be flexible, to make JSON structure changeable, create TaskJsonView object and add method to return the Map, not data class.
Create View to Return JSON
1 2 3 4 5 6 7 |
src - main - kotlin - com.example.myapp - presentation - task - TaskJsonView.kt |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.example.myapp.presentation.task import com.example.myapp.domain.task.Task object TaskJsonView { fun list(taskList: List<Task>): Map<String, Any?> { return mapOf( "data" to mapOf( "tasks" to taskList.map { mapOf( "id" to it.id, "name" to it.name, "created_at" to it.createdAt?.toString(), "updated_at" to it.updatedAt?.toString() ) } ) ) } } |
In Kotlin, we can define map with mapOf
function, so we can build the map corresponding to the JSON in looking JSON like structure. Of course, we can use MutableMap and add items one by one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
fun list(taskList: List<Task>): Map<String, Any?> { val taskMapList = mutableListOf<Map<String, Any?>>() taskList.forEach { val taskMap = mutableMapOf<String, Any?>() taskMap["id"] = it.id taskMap["name"] = it.name taskMap["created_at"] = it.createdAt taskMap["updated_at"] = it.updatedAt taskMapList.add(taskMap) } val dataMap = mutableMapOf<String, Any?>() dataMap["tasks"] = taskMapList val jsonMap = mutableMapOf<String, Any?>() jsonMap["data"] = dataMap return jsonMap } |
Use TaskJsonView in TaskController. Add the following method to TaskController.
1 2 3 4 5 |
@ResponseBody @GetMapping(produces = arrayOf("application/json")) fun listJson(): Map<String, Any?> { return TaskJsonView.list(taskRepository.findAll()) } |
Launch the server and send GET request to http://localhost:8080/task
for the JSON, then the following data will be returned. You must add "Accept=application/json"
to request header.
1 |
{"data":{"tasks":[{"id":10,"name":"First","created_at":"2017-11-17 10:56:35.555","updated_at":"2017-11-17 10:56:35.555"},{"id":11,"name":"Second","created_at":"2017-11-17 10:56:40.812","updated_at":"2017-11-17 10:56:40.812"},{"id":12,"name":"Third","created_at":"2017-11-17 10:56:43.102","updated_at":"2017-11-17 10:56:43.102"}]}} |
Here, the application returns actual data with the format {"data": { ... }}
. This structure is the same in the whole application, then let’s extract the structural part to LayoutJsonView.
1 2 3 4 5 6 7 |
src - main - kotlin - com.example.myapp |- presentation - layout - LayoutJsonView |
1 2 3 4 5 6 7 |
package com.example.myapp.presentation.layout object LayoutJsonView { fun success(data: Map<String, Any?>?): Map<String, Any?> { return mapOf("data" to data) } } |
Then, change TaskJsonView list method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fun list(taskList: List<Task>): Map<String, Any?> { return LayoutJsonView.success( mapOf( "tasks" to taskList.map { mapOf( "id" to it.id, "name" to it.name, "created_at" to it.createdAt?.toString(), "updated_at" to it.updatedAt?.toString() ) } ) ) } |
Continues to Web Application with Kotlin 1.1.61 and Spring (4 of 4).