728x90
Data Class를 추가하는 방식은 별다를 것 없이 Kotlin에서 하는 것과 동일하다.
아래와 같이, Message Controller와 같은 패키지에 Message라는 data class를 담은 Message.kt 파일을 추가해준다.
// Message.kt
package com.example.demo
data class Message(val id: String?, val text: String)
그리고 이를 이용해서 MessageController를 조금만 수정해주면, data class를 추가할 수 있다.
// MessageController.kt
package com.example.demo
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RequestMapping
@RestController
@RequestMapping("/")
class MessageController {
@GetMapping
fun listMessages() =listOf(
Message("1", "Hello!"),
Message("2", "Bonjour!"),
Message("3", "Privet!")
)
}
코드를 실행시킨 뒤 아래 링크로 접속해보면, JSON 형태로 된 메세지들을 확인할 수 있다.
http://localhost:8080
728x90
'웹 백엔드 개발 > Spring Boot' 카테고리의 다른 글
[Spring Booot] 백엔드 기본 개념 (DTO, Entity, DAO) (0) | 2025.01.11 |
---|---|
[Spring Boot] 스프링부트 기본 개념 (MVC, IoC, DI, AOP, PSA) (0) | 2025.01.07 |
[Spring Boot / Kotlin] 튜토리얼4, Spring Data CrudRepository 활용하기 (2) | 2025.01.07 |
[Spring Boot / Kotlin] 튜토리얼3, data base 추가하기 (0) | 2025.01.06 |
[Spring Boot / Kotlin] 튜토리얼1, Spring Boot 시작하기 (0) | 2025.01.04 |