[Web] REST-API(3) PUT 설명&예제

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

댓글

Designed by JB FACTORY