[Web] REST-API(3) PUT 설명&예제
- ETC.. / Web
- 2021. 3. 5.
728x90

PutApiController
@RestController
@RequestMapping("/api")
class PutApiController {
@PutMapping("/put-mapping")
fun putMapping(): String {
return "put-mapping"
}
@RequestMapping(method = [RequestMethod.PUT], path = ["/request-mapping"])
fun requestMapping(): String {
return "request-mapping - put method"
}
//Post와 동일, Put -> 내용없으면 생성, 있으면 수정
@PutMapping(path = ["/put-mapping/object"])
fun putMappingObject(@RequestBody userRequest: UserRequest): UserResponse {
//0. Response
return UserResponse().apply {
//1. result
this.result = Result().apply {
this.resultCode = "OK"
this.resultMessage = "성공"
}
}.apply {
//2. description
this.description = "~~~~~~~~~~~~"
}.apply {
//3. user mutable list
val userList = mutableListOf<UserRequest>()
userList.add(userRequest)
userList.add(UserRequest().apply {
this.name = "a"
this.age = 10
this.email = "a@gmail.com"
this.address = "a address"
this.phoneNumber = "010-1111-aaaa"
})
userList.add(UserRequest().apply {
this.name = "b"
this.age = 20
this.email = "b@gmail.com"
this.address = "b address"
this.phoneNumber = "010-1111-bbbb"
})
this.userRequest = userList
}
}
}
UserRequest
data class UserRequest(
var name: String? = null,
var age: Int? = null,
var email: String? = null,
var address: String? = null,
var phoneNumber: String? = null
)
UserResponse : 복잡한 Response 설계


data class UserResponse(
var result: Result? = null, //default = null
var description: String? = null,
@JsonProperty("user") //userRequest, user(response랑 request 이름 다를 경우)
var userRequest: MutableList<UserRequest>? = null
)
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy::class) //Camel Case -> Snake Case 변환(이름 맞추기)
data class Result(
var resultCode: String? = null,
var resultMessage: String? = null
)
728x90
'ETC.. > Web' 카테고리의 다른 글
[Web] REST-API(5) Response 설명&예제 (0) | 2021.03.07 |
---|---|
[Web] 리액티브 프로그래밍(Reactive Programming)이란? (0) | 2021.03.06 |
[Web] REST-API(2) POST 설명&예제 (0) | 2021.03.03 |
[Web] REST-API(1) GET 설명&예제 (0) | 2021.03.02 |
[Web] REST의 기본개념 & "RESTful 하다" 라는 의미 (0) | 2021.02.27 |