๐ŸŒฑ Spring MVC (3) HttpRequest ๋‹ค๋ฃจ๊ธฐ - Http Request handling

Kim Dae Hyunยท2021๋…„ 7์›” 6์ผ
2

Spring-MVC

๋ชฉ๋ก ๋ณด๊ธฐ
2/13
post-thumbnail

Github ์†Œ์Šค์ฝ”๋“œ

๐Ÿ”Ž ์ด๋Ÿฐ๊ฑฐ ํ•ด๋ณผ ๊ฑฐ์—์š”.

  • HTTP ์š”์ฒญ header ํŒŒ์‹ฑ
  • HTTP ์š”์ฒญ body ํŒŒ์‹ฑ
    • ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ (GET, POST)
    • ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ ๊ฐ์ฒด๋กœ ๋ฐ›๋Š” ๊ฒฝ์šฐ (GET, POST)
    • HTTP body์— String(text)์ด ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ
    • HTTP body์— Jsonํ˜•ํƒœ ๊ฐ’์ด ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ
    • ์š”์ฒญ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด๋กœ ๋ฐ›๋Š” ๊ฒฝ์šฐ

HTTP ์š”์ฒญ Header ํŒŒ์‹ฑ

  • @Slf4j (Lombok) : Logger๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ
  • @RestController : ๋ฆฌํ„ด๋˜๋Š” ๊ฐ’์„ HTTP์‘๋‹ต์˜ body๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•จ
  • ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด Postman ์‚ฌ์šฉ
@Slf4j
@RestController
public class RequestHeaderController {

    @GetMapping(value = "/headers")
    public String headers(
            HttpServletRequest request,
            HttpServletResponse response,
            HttpMethod httpMethod,
            Locale locale, 
            @RequestHeader MultiValueMap<String, String> headerMap,                
            @RequestHeader("host") String host, 
            @CookieValue(value="myCookie", required = false) String cookie
            ) {
        log.info("request={}", request);
        log.info("response={}", response);
        log.info("httpMethod={}", httpMethod);
        log.info("locale={}", locale);
        log.info("headerMap={}", headerMap);
        log.info("header host={}", host);
        log.info("myCookie={}", cookie);

        return "ok";
    }
}
  • Postman์„ ์ด์šฉํ•œ API ํ˜ธ์ถœ

  • ํ˜ธ์ถœ ํ›„ Console์˜ Log

  • HttpMethod

    • ์š”์ฒญ์˜ HTTP Method ํ™•์ธ (GET, POST, DELETE, FETCH ... )
  • Locale

    • ์–ธ์–ด์ •๋ณด ํ™•์ธ
  • @RequestHeader MultiValueMap<String, String>

    • ์š”์ฒญ๋œ ํ—ค๋”์˜ ๋ชจ๋“  ์ •๋ณด
    • (MultiValueMap: ๊ฐ™์€ Key๊ฐ’์— ๋Œ€ํ•ด ์ค‘๋ณต๋œ Value๋ฅผ ๊ฐ–์„ ์ˆ˜ ์žˆ๋Š” Map)
  • @RequestHeader("host") String host

    • ํ™•์ธํ•˜๊ณ ์ž ํ•˜๋Š” ํ—ค๋”์˜ ์†์„ฑ ์ง€์ •
  • @CookieValue(value="myCookie", required = false) String cookie

    • Cookie ๊ฐ’ ํ™•์ธ
    • Cookie๊ฐ’ ํ™•์ธ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ๋งคํ•‘ ์ถ”๊ฐ€
      • @GetMapping("/add-cookie")
        public String addCookie(HttpServletRequest request, HttpServletResponse response) {
            Cookie cookie = new Cookie("myCookie", "myCookieValue");
            response.addCookie(cookie);
            return "ok";
        }
    • /add-cookie๋กœ GET์š”์ฒญ ํ›„ /headers๋กœ ์žฌ์š”์ฒญ ๊ฒฐ๊ณผ

์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ GET, POST

GET ๋ฐฉ์‹์œผ๋กœ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ํ†ตํ•ด ์ „๋‹ฌ๋˜๋Š” ๊ฐ’์„ ์–ป์–ด์˜ค๋Š” ๊ฒƒ๊ณผ
POST ๋ฐฉ์‹์œผ๋กœ ํผ์„ ํ†ตํ•ด ์ „๋‹ฌ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์–ป์–ด์˜ค๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

  • ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฒฝ์šฐ ์–ด๋–ค ๋ฐฉ์‹์ด๋“  ์–ป์–ด์˜ค๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด์œ ๋Š” GET ๋ฐฉ์‹์ด๋“  POST ๋ฐฉ์‹์ด๋“  ์ „๋‹ฌ๋˜๋Š” ํ˜•์‹์ด ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
  • GET ๋ฐฉ์‹์˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฒฝ์šฐ username์— kim์„ age์— 10์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ
    • localhost:8080/request?username=kim&age=10
  • POST ๋ฐฉ์‹์˜ ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฒฝ์šฐ username์— kim์„ age์— 10์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ
    • POST์˜ ๊ฒฝ์šฐ username=kim&age=10์ด ๋ถ€๋ถ„์„ ๊ทธ๋Œ€๋กœ body์— ๋„ฃ์–ด ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฒฐ๊ตญ ๋ฐ์ดํ„ฐ๊ฐ€ ๋‹ด๊ธด ์œ„์น˜? ๊ฐ€ ๋‹ค๋ฅธ ๊ฒƒ ๋ฟ์ด์ง€ ํ˜•ํƒœ๋Š” ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ• 1 (@RequestParam)

  • @RequestParam("username") Sring username

    • username์„ key๊ฐ’์œผ๋กœ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ String ํƒ€์ž…์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • @RequestParam("age") int age

    • age๋ฅผ key๊ฐ’์œผ๋กœ ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ int ํƒ€์ž…์œผ๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  • Postman์„ ์ด์šฉํ•ด localhost:8080/request-param-v2?username=kim&age=10๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ

    ๐Ÿš€ ์ดํ›„ ๋ชจ๋“  ๋ฐฉ์‹์˜ Log๊ฒฐ๊ณผ๋Š” ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ ํ™•์ธ์€ ์ƒ๋žตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ใ…Ž

    @ResponseBody 
    @RequestMapping("/request-param-v2")
    public String requestParamV2(@RequestParam("username") String username, @RequestParam("age") int age) {
        log.info("username = {}", username);
        log.info("age = {}", age);

        return "ok";
    }

๋ฐฉ๋ฒ• 2 (@RequestParam key๊ฐ’ ์ƒ๋žต)

  • @RequestParam์˜ key๊ฐ’์„ ์ƒ๋žตํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.
  • ํŒŒ๋ผ๋ฏธํ„ฐ๋ช…์„ key๊ฐ’์œผ๋กœ ์ด์šฉํ•˜๋Š” ๋ฐฉ์‹์ด๋ฏ€๋กœ ํŒŒ๋ผ๋ฏธํ„ฐ๋ช…๊ณผ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๋ช…์„ ์ผ์น˜์‹œ์ผœ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด ๋ฐฉ์‹์ด ์กฐ๊ธˆ ๋” ๋ณด๊ธฐ ์ข‹๋„ค์š”.
    @ResponseBody
    @RequestMapping("/request-param-v3")
    public String requestParamV3(@RequestParam String username, @RequestParam int age) {
        log.info("username = {}", username);
        log.info("age = {}", age);

        return "ok";
    }

๋ฐฉ๋ฒ• 3 (@RequestParam Required)

  • Required ์†์„ฑ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. default๋Š” true ์ž…๋‹ˆ๋‹ค.
  • false๋กœ ์„ค์ •์‹œ ์ฃผ์˜ํ•ด์•ผ ํ•  ์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
    • primitive ํƒ€์ž… ์‚ฌ์šฉ์— ์œ ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. primitive ํƒ€์ž…์€ int, boolean, double, long ๋“ฑ์˜ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ œ๊ณต๋˜๋Š” ํƒ€์ž…์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
    • primitive ํƒ€์ž…์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ false๋กœ ์„ค์ •์‹œ null ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ•˜๊ณ  5xx ์„œ๋ฒ„์ธก ์—๋Ÿฌ์ฝ”๋“œ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
    • primitive ํƒ€์ž…์— Required false ์„ค์ •์‹œ ํด๋ผ์ด์–ธํŠธ์ธก validation, ์„œ๋ฒ„์ธก validation, Integer, Double ๋“ฑ Wrapper ํด๋ž˜์Šค ์‚ฌ์šฉ ํ˜น์€ @RequestParam์˜ defaultValue ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    @ResponseBody
    @RequestMapping("/request-param-required")
    public String requestParamRequired(
            @RequestParam(required = true) String username,
            @RequestParam(required = false) int age) {
        log.info("username = {}", username);
        log.info("age = {}", age);

        return "ok";
    }

์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ ๊ฐ์ฒด๋กœ ๋ฐ›๋Š” ๊ฒฝ์šฐ (GET, POST)

GET๋ฐฉ์‹์˜ Query-parameter , POST๋ฐฉ์‹์˜ ํผ ๋ฐ์ดํ„ฐ๋ฅผ Object๋กœ ๋ฐ›๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ• 1 ( @ModelAttribute + Object )

  • ์‚ฌ์šฉ๋œ ๊ฐ์ฒด UserDto ํด๋ž˜์Šค ์ž…๋‹ˆ๋‹ค.
@Data
public class UserDto {

    private String username;
    private int age;
}
  • @ModelAttribute ๋Š” ์ „๋‹ฌ๋œ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ key๊ฐ’์„ ๊ฐ์ฒด์˜ ํ•„๋“œ๋ช…๊ณผ ๋งคํ•‘ํ•˜์—ฌ ํ•ด๋‹น ํ•„๋“œ์˜ setter๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ฐ’์„ ๋ฐ”์ธ๋”ฉํ•ฉ๋‹ˆ๋‹ค.
  • @ModelAttribute ๋Š” ๊ฐ’์„ ๋งคํ•‘ํ•˜์—ฌ ์„ธํŒ…ํ•ด์ฃผ๋Š” ๊ฒƒ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ View๋‹จ์œผ๋กœ ๊ฐ’์„ ๋„˜๊ธธ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” Model์— ๊ฐ’์„ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค. ์ดํ›„์— ์ž์„ธํžˆ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
@RequestMapping("/model-attribute-v1")
public String modelAttributeV1(@ModelAttribute UserDto userDto) {
    log.info("username = {}", userDto.getUsername());
    log.info("age = {}", userDto.getAge());

    return "ok";
}

๋ฐฉ๋ฒ• 2 ( @ModelAttribute ์ƒ๋žต )

  • @ModelAttribute ๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, Object์ธ ๊ฒฝ์šฐ์—๋งŒ ๋ฌต์‹œ์ ์œผ๋กœ @ModelAttribute๊ฐ€ ๋ถ™์Šต๋‹ˆ๋‹ค.
  • primitiveํƒ€์ž…์˜ ๊ฒฝ์šฐ @RequestParam์ด ๋ถ™๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
@RequestMapping("/model-attribute-v2")
public String modelAttributeV2(UserDto userDto) {
     log.info("username = {}", userDto.getUsername());
     log.info("age = {}", userDto.getAge());

     return "ok";
}

HTTP body์— String(text)์ด ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ

POST ๋ฐฉ์‹์œผ๋กœ Http Body์— row๋ฐ์ดํ„ฐ๋กœ text๊ฐ€ ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ์˜ ๋ฐ์ดํ„ฐ ํŒŒ์‹ฑ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ• 1 ( HttpEntity )

  • HttpEntity ๋ฅผ ํ†ตํ•ด Http ๋ฉ”์‹œ์ง€์— ์ง์ ‘ ์ ‘๊ทผํ•˜์—ฌ body ๋ฉ”์‹œ์ง€๋ฅผ ํŒŒ์‹ฑํ•ฉ๋‹ˆ๋‹ค.
  • HttpEntity๋Š” body ์ •๋ณด ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ header์ •๋ณด๊นŒ์ง€ ํŒŒ์‹ฑ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. Http messgae์— ์ง์ ‘ ์ ‘๊ทผํ•˜๊ธฐ ๋•Œ๋ฌธ์ด์ง€์š” ๐Ÿ˜
   @PostMapping("/request-body-string-v3")
    public HttpEntity<String> requestBodyStringV3(HttpEntity<String> httpEntity) throws IOException {
        String str = httpEntity.getBody();
        log.info("body string = {}", str);

        return new HttpEntity<>("ok");
    }

๋ฐฉ๋ฒ• 2 ( RequestEntity )

  • HttpEntity ๋ฅผ ์ƒ์†ํ•˜๋Š” RequestEntity๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
  • HttpEntity์™€ ๊ฑฐ์˜ ๊ธฐ๋Šฅ์ด ๊ฐ™์ง€๋งŒ RequestEntity ๋Š” ๋ถ€๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. (url ์ •๋ณด ๋“ฑ ...)
  • ResponseEntity ๋˜ํ•œ HttpEntity๋ฅผ ์ƒ์†ํ•˜๋Š” ํด๋ž˜์Šค๋กœ ๊ฑฐ์˜ ๊ฐ™์€ ๊ธฐ๋Šฅ์ด๊ณ  ๋ถ€๊ฐ€์ ์ธ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
    @PostMapping("/request-body-string-v4")
    public ResponseEntity<String> requestBodyStringV4(RequestEntity<String> requestEntity) throws IOException {
        String str = requestEntity.getBody();

        log.info("body string = {}", str);
        log.info("request url = {}", requestEntity.getUrl());

        return new ResponseEntity<String>("ok", HttpStatus.OK);
    }

๋ฐฉ๋ฒ• 3 ( RequestBody )

  • ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ( ๊ถŒ ์žฅ ๐Ÿ‘)
  • @RequestBody ๋ฅผ ์ด์šฉํ•˜์—ฌ Http message์˜ body๋ถ€๋ถ„์„ ๋ฐ”๋กœ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    @ResponseBody
    @PostMapping("/request-body-string-v5")
    public String requestBodyStringV5(@RequestBody String str) {

        log.info("body string = {}", str);

        return "ok";
    }

HTTP body์— Jsonํ˜•ํƒœ ๊ฐ’์ด ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ

  • POST ๋ฐฉ์‹์œผ๋กœ body์— Json ํƒ€์ž…์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์ „์†ก๋˜๋Š” ๊ฒฝ์šฐ ํŒŒ์‹ฑ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
  • {"username":"kim", "age":10} ์ด๋Ÿฐ์‹์œผ๋กœ ์ „๋‹ฌ๋˜๋Š” ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค.

๋ฐฉ๋ฒ• 1 ( @RequestBody + String + ObjectMapper)

  • Stringํƒ€์ž… ๋ฐ์ดํ„ฐ๋ฅผ Object๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ObjectMapper๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • @RequestBody๋ฅผ ์ด์šฉํ•ด String์œผ๋กœ Jsonํƒ€์ž… body๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ฒŒ ๋˜๋ฉด {"username":"kim", "age":10} ์ด๋Ÿฐ Json๋ชจ์–‘์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ทธ๋Œ€๋กœ String์œผ๋กœ ๋“ค์–ด์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
    • log.info("ObjectMapper ์ด์ „ bodyString = {}", bodyString); ๊ทธ๋Œ€๋กœ ๋กœ๊ทธ๋ฅผ ์ฐ์€ ๊ฒฐ๊ณผ์ž…๋‹ˆ๋‹ค.
  • ์ด๋Ÿฐ Jsonํ˜•ํƒœ์˜ String ๋ฐ์ดํ„ฐ๋ฅผ ObjectMapper์˜ readValue๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • โ—๏ธ @RequestBody ์‚ฌ์šฉ์‹œ ์ฃผ์˜
    • @RequestBody๋Š” ์ƒ๋žต ๋ถˆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
    • ์ƒ๋žต์‹œ ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ @ModelAttribute๊ฐ€ primitiveํƒ€์ž…์˜ ๊ฒฝ์šฐ @RequestParam์ด ๊ธฐ๋ณธ์œผ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

private ObjectMapper objectMapper = new ObjectMapper();

@ResponseBody
@PostMapping("/request-body-json-v3")
public String requestBodyJsonV3(@RequestBody String bodyString) throws JsonProcessingException {

    UserDto userDto = objectMapper.readValue(bodyString, UserDto.class);

    log.info("username = {}", userDto.getUsername());
    log.info("age = {}", userDto.getAge());

    return "ok";
}

๋ฐฉ๋ฒ• 2 ( @RequestBody + Object )

  • ์ด์ „์—๋Š” @RequestBody๋กœ Stringํƒ€์ž…์„ ๋ฐ›์•˜์ง€๋งŒ ์ด๋ฒˆ์—” Object๋กœ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.
  • ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋”ฐ๋กœ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์ด ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ObjectMapper๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋˜์ง€์š”.
  • ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•˜๋ฏ€๋กœ ๊ถŒ์žฅ๋˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๐Ÿ‘
@ResponseBody
@PostMapping("/request-body-json-v4")
public String requestBodyJsonV4(@RequestBody UserDto userDto) {
    log.info("username = {}", userDto.getUsername());
    log.info("age = {}", userDto.getAge());

    return "ok";
}

๋ฐฉ๋ฒ• 3 ( RequestEntity )

  • RequestEntity๋ฅผ ์ด์šฉํ•ด Http ์š”์ฒญ ์ž์ฒด๋ฅผ ๋ฐ›์•„๋“ค์ด๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
  • ์ด ๋ฐฉ๋ฒ•์˜ ๊ฒฝ์šฐ body ๋ฐ์ดํ„ฐ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ Http์š”์ฒญ์˜ ํ—ค๋”, url ๋“ฑ์˜ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • RequestEntity๋ฅผ ์‚ฌ์šฉํ–ˆ์œผ๋ฏ€๋กœ ๋ฆฌํ„ด๋„ ResponseEntity๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. String์„ ๋ฆฌํ„ดํ•ด๋„ ์ƒ๊ด€ ์—†์Šต๋‹ˆ๋‹ค.
@ResponseBody
@PostMapping("/request-body-json-v5")
public ResponseEntity<String> requestBodyJsonV5(RequestEntity<UserDto> requestEntity) {
    log.info("username = {}", requestEntity.getBody().getUsername());
    log.info("age = {}", requestEntity.getBody().getAge());
        
    return new ResponseEntity<>("ok", HttpStatus.OK);
    }
profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€