ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • HTTP API ์‘๋‹ต ์ •๋ฆฌ
    ๊ฐœ๋ฐœ/Spring Boot 2022. 7. 7. 02:28

     

     

     

    HTTP ๋ฉ”์„ธ์ง€ ๋ฐ”๋””๋ฅผ ์ง์ ‘ ์กฐํšŒํ•˜๊ณ  ์‹ถ์„ ๋•Œ, ์Šคํ”„๋ง MVC๋Š” ๋‹ค์Œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ง€์›ํ•œ๋‹ค.

     

    HttpEntity


    HTTP header, body ์ •๋ณด๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    - ๋ฉ”์‹œ์ง€ ๋ฐ”๋”” ์ •๋ณด๋ฅผ ์ง์ ‘ ์กฐํšŒ

    - ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ๊ณผ ๊ด€๊ณ„ ์—†์Œ

      (@RequestParam X, @ModelAttribute X HttpEntity๋Š” ์‘๋‹ต์—๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ)

    - ๋ฉ”์‹œ์ง€ ๋ฐ”๋”” ์ •๋ณด ์ง์ ‘ ๋ฐ˜ํ™˜, ํ—ค๋” ์ •๋ณด ํฌํ•จ ๊ฐ€๋Šฅ

    - view ์กฐํšŒX

     

     

    +) HttpEntity ๋ฅผ ์ƒ์†๋ฐ›์€ ๋‹ค์Œ ๊ฐ์ฒด๋“ค๋„ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

     

    RequestEntity

    HttpMethod, url ์ •๋ณด๊ฐ€ ์ถ”๊ฐ€, ์š”์ฒญ์—์„œ ์‚ฌ์šฉ

     

    ResponseEntity

    HTTP ์ƒํƒœ ์ฝ”๋“œ ์„ค์ • ๊ฐ€๋Šฅ, ์‘๋‹ต์—์„œ ์‚ฌ์šฉ

     

    return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED)
     

     

    ์Šคํ”„๋งMVC ๋‚ด๋ถ€์—์„œ HTTP ๋ฉ”์‹œ์ง€ ๋ฐ”๋””๋ฅผ ์ฝ์–ด์„œ ๋ฌธ์ž๋‚˜ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ „๋‹ฌํ•ด์ฃผ๋Š”๋ฐ, ์ด๋•Œ HTTP ๋ฉ”์‹œ์ง€ ์ปจ๋ฒ„ํ„ฐ( HttpMessageConverter )๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•œ๋‹ค

     

     

    @RequestBody


    @RequestBody ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด HTTP ๋ฉ”์‹œ์ง€ ๋ฐ”๋”” ์ •๋ณด๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์ฐธ๊ณ ๋กœ ํ—ค๋” ์ •๋ณด๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด HttpEntity ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ @RequestHeader ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.
    ์ด๋ ‡๊ฒŒ ๋ฉ”์‹œ์ง€ ๋ฐ”๋””๋ฅผ ์ง์ ‘ ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ์€ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” @RequestParam , @ModelAttribute ์™€๋Š” ์ „ํ˜€ ๊ด€๊ณ„๊ฐ€ ์—†๋‹ค.

     

    @ResponseBody


    @ResponseBody ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‘๋‹ต ๊ฒฐ๊ณผ๋ฅผ HTTP ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ์ง์ ‘ ๋‹ด์•„์„œ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฌผ๋ก  ์ด ๊ฒฝ์šฐ์—๋„ view๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

    @ResponseBody ์ธ ๊ฒฝ์šฐ์—” ์‘๋‹ต์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์—†๋‹ค. ๋งŒ์•ฝ ์‘๋‹ต์ฝ”๋“œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด @ResponseStatus๋‚˜ ResponseEntity๋ฅผ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ฐธ๊ณ ๋กœ ResponseEntity๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์—” @ResponseBody๊ฐ€ ์—†์–ด๋„ view resolver๋ฅผ ๊ฑฐ์น˜์ง€ ์•Š๋Š”๋‹ค.

     

     //๋™์ ์œผ๋กœ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ด์„œ ์‘๋‹ตํ•˜๋Š” ๊ฒฝ์šฐ
     @GetMapping("/response-body-json-v1")
        public ResponseEntity<HelloData> responseBodyJsonV1() {
            HelloData helloData = new HelloData();
            helloData.setUsername("userA");
            helloData.setAge(20);
    
            return new ResponseEntity<>(helloData, HttpStatus.OK);
        }
    
        //ํ•œ๊ฐ€์ง€ ์‘๋‹ต๋งŒ ์„ค์ •ํ•  ๊ฒฝ์šฐ
        @ResponseStatus(HttpStatus.OK)
        @GetMapping("/response-body-json-v2")
        public @ResponseBody HelloData responseBodyJsonV2() {
            HelloData helloData = new HelloData();
            helloData.setUsername("userA");
            helloData.setAge(20);
    
            return helloData;
        }

     

    ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ vs HTTP ๋ฉ”์‹œ์ง€ ๋ฐ”๋””


     

    ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ: @RequestParam , @ModelAttribute

    HTTP ๋ฉ”์‹œ์ง€ ๋ฐ”๋””๋ฅผ ์ง์ ‘ ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ: @RequestBody

     

     

     

     

     

    ๋Œ“๊ธ€

Designed by Tistory.