Home > Archive > Spring & Spring Boots > ๐Ÿƒ[Spring] API

๐Ÿƒ[Spring] API
Spring Framework

API

@ResponseBody ๋ฌธ์ž ๋ฐ˜ํ™˜

@Controller
public class HelloController {
    
    @GetMapping("hello-string")
    @ResponseBody
    public String helloString(@RequestParam("name") String name) {
        return "hello " + name;
    }
}
  • @ResponseBody๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ทฐ ๋ฆฌ์กธ๋ฒ„('viewResolver')๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค
    • ๋Œ€์‹  HTTP์˜ BODY์— ๋ฌธ์ž ๋‚ด์šฉ์„ ์ง์ ‘ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค(HTML BODY TAG๋ฅผ ๋งํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค.)

์‹คํ–‰
http://localhost:8080/hello-string?name=spring

@ResponseBody ๊ฐ์ฒด ๋ฐ˜ํ™˜

@Controller
public class HelloController {
    
    @GetMapping("hello-api")
    @ResponseBody
    public Hello helloApi(@RequestParam("name") String name) {
        Hello hello = new Hello();
        hello.setName(name);
        return hello;
    }
    
    static class Hello {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
  • @ResponseBody๋ฅผ ์‚ฌ์šฉํ•˜์š”, ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๊ฐ์ฒด๊ฐ€ JSON์œผ๋กœ ๋ณ€ํ™˜๋ฉ๋‹ˆ๋‹ค.

์‹คํ–‰

  • http://localhost:8080/hello-api?name=spring

@ResponseBody ์‚ฌ์šฉ ์›๋ฆฌ

  1. ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ localhost:8080/hello-api ๋กœ ์ ‘์†์„ ์‹œ๋„ํ•ฉ๋‹ˆ๋‹ค.
  2. ํ†ฐ์ผ“ ๋‚ด์žฅ ์„œ๋ฒ„์—์„œ โ€œhello-api ๊ฐ€ ์™”์–ดโ€ ํ•˜๊ณ  ์Šคํ”„๋ง์— ๋˜์ง‘๋‹ˆ๋‹ค.
  3. ์Šคํ”„๋ง์€ โ€œhello-api ๊ฐ€ ์žˆ๋„ค?! ์–ด? ๊ทผ๋ฐ @RespinseBody ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์–ด์žˆ๋‹ค!!โ€ ํ•˜๊ณ  ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋Ÿฐ ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์–ด์žˆ์ง€ ์•Š์•˜๋‹ค๋ฉด viewResolver์—๊ฒŒ ๋˜์ง‘๋‹ˆ๋‹ค. -> โ€œ๋‚˜์—๊ฒŒ ๋งž๋Š” ํ…œํ”Œ๋ฆฟ์„ ์ฐพ์•„ ๋Œ๋ ค์ค˜โ€
    • @ResponsBody๊ฐ€ ์žˆ๋‹ค๋ฉด HTTP ์‘๋‹ต์— ๊ทธ๋Œ€๋กœ ๋„˜๊ฒจ์•ผ๊ฒ ๋‹ค ํ•˜๊ณ  ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.
      • ์—ฌ๊ธฐ์„œ ๋ฌธ์ž๊ฐ€ ์˜ค๋ฉด ๋ฐ”๋กœ ๋ฌธ์ž๋ฅผ ๋Œ๋ ค์ค๋‹ˆ๋‹ค.
      • ํ•˜์ง€๋งŒ ๊ฐ์ฒด๊ฐ€ ์˜ค๋ฉด ๊ธฐ๋ณธ ๋””ํดํŠธ์ธ JSON ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์„œ HTTP ์‘๋‹ต์— ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. -> ๊ธฐ๋ณธ ์ •์ฑ…
  4. @ResponseBody return: hello(name: spring)์€ ๊ฐ์ฒด๋ฅผ ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด โ€œHttpMessageConverterโ€œ ๊ฐ€ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  5. ๋งŒ์•ฝ ๋ฐ˜ํ™˜ํ•˜๋Š” ์•„์ด๊ฐ€ ๋‹จ์ˆœ ๋ฌธ์ž์ผ ๊ฒฝ์šฐ โ€œStringConverterโ€œ ๊ฐ€ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  6. ๊ทธ๊ฒŒ ์•„๋‹ˆ๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ์•„์ด๊ฐ€ ๊ฐ์ฒด์ผ ๊ฒฝ์šฐ โ€œJsonConverterโ€œ ๊ฐ€ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  7. ๊ฒฐ๊ตญ JSON ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•œ ๊ฒƒ์„ ์š”์ฒญํ•œ ์›น ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ๋ณด๋‚ด์ค๋‹ˆ๋‹ค.

์ •๋ฆฌ

  • @ResponseBody๋ฅผ ์‚ฌ์šฉ
    • HTTP์˜ BODY์— ๋ฌธ์ž ๋‚ด์šฉ์„ ์ง์ ‘ ๋ฐ˜ํ™˜
    • viewResolver ๋Œ€์‹ ์— httpMessageConverter๊ฐ€ ๋™์ž‘
    • ๊ธฐ๋ณธ ๋ฌธ์ž์ฒ˜๋ฆฌ: StringHttpMessageConverter
    • ๊ธฐ๋ณธ ๊ฐ์ฒด์ฒ˜๋ฆฌ: MappingJackson2HttpMessageConverter
    • byte ์ฒ˜๋ฆฌ ๋“ฑ๋“ฑ ๊ธฐํƒ€ ์—ฌ๋Ÿฌ HttpMessageConverter๊ฐ€ ๊ธฐ๋ณธ์œผ๋กœ ๋“ฑ๋ก๋˜์–ด ์žˆ์Œ

์ฐธ๊ณ : ํด๋ผ์ด์–ธํŠธ HTTP Accept ํ—ค๋”์™€ ์„œ๋ฒ„์˜ ์ปจํŠธ๋กค๋Ÿฌ ๋ฐ˜ํ™˜ ํƒ€์ž… ์ •๋ณด ๋“ค์„ ์กฐํ•ฉํ•ด์„œ HttpMessageConverter๊ฐ€ ์„ ํƒ๋ฉ๋‹ˆ๋‹ค.