The code I saw in "Spring with Kotorin-- 1. SPRING INITIALIZR" was automatically generated by SPRING INITIALIZR and there is no particular processing. It was a thing.
So this time, I will add the following very simple process.
--Add controller for REST access: ** RestController ** --Adding entities for information to display on access: ** Data Class **
RestController Create a class with ** @ RestController ** annotations to define the RestController class.
RestController
@RestController
class SimpleController {
...
}
By the way, this @RestController is a combination of ** @ Controller ** and ** @ ResponseBody **. Therefore, it behaves in the same way even if it is defined as follows.
Controller
@Controller
@ResponseBody
class SimpleController {
...
}
RequestMapping ** @RequestMapping ** Annotate and map to the request from the client.
Specify the mapping condition using the following attributes.
GetMapping
The ** @GetMapping ** annotation is an annotation that acts as a shortcut for @RequestMapping (method = RequestMethod.GET)
.
Data Class A data class is a class that is used to hold only data without processing. The data class is defined in the following format.
-** data ** Add keyword before class --Define the primary constructor
DataClass
data class Data(var id: String, var value: String)
The compiler automatically infers and generates the following processing from the contents defined as the data class.
Spring Dependencies
Data Class
data class Message(var id: String,
var title: String,
var message: String)
You can see that the description is simple without getter, setter, toString, and equals that were described when defining the definition of JavaBeans in Java. A similar description was achieved in Java by using ** Lombok **, but Kotlin supports this format at the language level.
RestController
import io.pivotal.syanagihara.simple.data.Message
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.text.SimpleDateFormat
import java.util.*
@RestController
@RequestMapping("/simple")
class SimpleController {
@GetMapping
fun getMessages() : List<Message> {
return listOf(
Message(
UUID.randomUUID().toString(),
"First Message",
"This is a 1st message on ${getDate()}."
)
)
}
private fun getDate() : String {
val simpleDateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
return simpleDateFormat.format(Date())
}
}
I am using the ** java.util.Date ** class to print the current date and time. In this way, Java API calls from Kotlin can be made transparently.
I have declared getDate () private, but I will briefly summarize Kotlin's access modifiers.
Access modifier | Contents |
---|---|
public | Accessable from all classes |
internal | Accessable from classes in the same module |
protected | Only accessible from subclasses |
private | Only accessible from the declared class |
When accessing this application, the following result is returned.
$ curl http://localhost:8080/simple
[{"id":"2ac2bf0a-8276-43e2-89d2-c0eb70ad38d9","title":"First Message","message":"This is a 1st message on 2018/11/04 20:52:45."},{"id":"04e915e4-f4fb-4920-84c9-f411f7603044","title":"Second Message","message":"This is a 2nd message on 2018/11/04 20:52:45."}]
I find Lombok + Java useful, but I find Kotlin, which offers Lombok-equivalent functionality at the language level, even more useful. Also, this time I made a simple application using RestController. It's very simple and easy with Spring + Kotlin without any unnecessary description.
Recommended Posts