Spring with Kotorin --3. Omitting curly braces from the function

Overview / Description

In Spring with Kotorin-- 2. RestController and Data Class, I added a controller class to enable REST API access. However, the API I added was a very simple application with only GET access.

This time, let's add an API so that it can be accessed by the HTTP method with PUT / POST / DELETE.

Among them, I will introduce the omission of curly braces in one of the ways to write a function in Kotlin.

Assumptions / Environment

Runtime version

Spring Dependencies

Development environment

Other

--API design ――For the sake of clarity, we have not designed the API properly. Each access path is given to each API of GET / PUT / POST / DELETE.

Procedure / Explanation

In the API we prepared for GET access earlier, we used the @GetMapping annotation as follows:

@GetMapping(value = ["/display"])
fun getMessages() : List<Message> {...}

Similarly, PUT / POST / DELETE are defined using @ PutMapping / @PostMapping / @DeleteMapping.

@PutMapping(value = ["/insert"])
fun insertMessage(@RequestBody message: Message) : Message {...}

@PostMapping(value = ["/update"])
fun updateMessage(@RequestBody message: Message) : Message {...}

@DeleteMapping(value = ["/delete/{id}"])
fun deleteMessage(@PathVariable(name = "id") id: String): Boolean = {...}

Omission of curly braces from function

Normally, when defining a function, enclose it in {...} ** curly braces ** and write the expression to be processed in it. In Kotlin, if there is only one expression, you can omit the curly braces. Therefore, the following two functions have the same meaning.

With curly braces


@DeleteMapping(value = ["/delete/{id}"])
fun deleteMessage(@PathVariable(name = "id") id: String): Boolean {
  return true
}

Braces omitted


@DeleteMapping(value = ["/delete/{id}"])
fun deleteMessage(@PathVariable(name = "id") id: String): Boolean = true

Summary / Looking back

This time, I introduced the description that omits the curly braces in the function, but in Kotlin, you can write various other omitted descriptions, and you can write the code concisely.

In the future, I will continue to explain the parts that can be described concisely while creating applications with Spring.

This source

Recommended Posts

Spring with Kotorin --3. Omitting curly braces from the function
Spring with Kotorin --- 5. Actuator
With Kotorin ―― 7. Scoping Function
Spring with Kotorin ―― 1. SPRING INITIALIZR
Spring with Kotorin --8 Repository layer
Spring with Kotorin --6 Asynchronous processing
Spring with Kotorin ―― 7. Service layer
Login function with Spring Security
Spring with Kotorin --4 REST API design
Implemented authentication function with Spring Security ②
Implemented authentication function with Spring Security ③
Implemented authentication function with Spring Security ①
Spring with Kotorin --9 Database migration --Flyway
How to access Socket directly with the TCP function of Spring Integration
SetCookie from the client side with OkHttp3
Spring with Kotorin --2 RestController and Data Class
Implement paging function with Spring Boot + Thymeleaf
Resource handler settings when delivering SPA with the static resource function of Spring Boot
[Java] Set the time from the browser with jsoup
Customize the output with Wagby's CSV download function
Try calling the CORBA service from Spring (Java)
Try to implement login function with Spring Boot
Spring with Kotorin --8 Repository layer --Supplement: H2 Database
Matches annotations on the interface with Spring AOP
[Introduction to Spring Boot] Authentication function with Spring Security