[spring boot] RequestMapping - POST

Darcy Daeseok YU ·2022년 12월 19일
0

RequestMapping
POST

PostMapping

파라미터가 Http body에 담겨서 서버에 요청이 들어옴

@RequestBody를 이용하여 body에 담긴 파라미터 값을 받아야 함.


    @RequestMapping(value = ["/"], method = [RequestMethod.POST])
    fun greetingUser(): String {
        return "postMethod, Good to see you"
    }

    @PostMapping("")
    fun greetingUser(@RequestBody name: String): String {
        return "postMethod, Good to see you, name: ${name}"
    }

    @PostMapping("map")
    fun postUser(@RequestBody data: Map<String, Object>): String {

        val sb = StringBuilder()

        for (entry in data.entries) {
            sb.append("${entry.key} = ${entry.value}, ")
        }
        return "Done:Map:: ${sb.toString()}"
    }

    @PostMapping("dto")
    fun postDto(@RequestBody user: UserDTO): String {
        return "Done:DTO:: ${user.toString()}"
    }

주의: DTO 사용할때 멤버필드 변수값 필수값 또는 옵셔날 값인지 체크해서 보내야함.
필수값이 비었을 경우 Bad Request 오류 발생

profile
React, React-Native https://darcyu83.netlify.app/

0개의 댓글