๐ŸŒฑ Spring MVC (4) Response (ViewResolver, ModelAndView, ํด๋ผ์ด์–ธํŠธ๋กœ ์‘๋‹ต ๋ฐฉ๋ฒ•)

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

Spring-MVC

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

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

๐Ÿ”Ž View Resolver

Spring MVC๋Š” ๋žœ๋”๋งํ•˜๊ณ ์ž ํ•˜๋Š” View์˜ ๋…ผ๋ฆฌ์  ์ด๋ฆ„๋งŒ ์ œ๊ณตํ•˜๋ฉด ํ•ด๋‹น View์˜ ๋ฌผ๋ฆฌ์  ์œ„์น˜๋ฅผ ์ฐพ์•„ ๋žœ๋”๋งํ•ด์ค€๋‹ค. ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์„ View Resolver๊ฐ€ ์ œ๊ณตํ•œ๋‹ค.

  • ํ”ํžˆ Spring MVC์™€ Template Engine(Thymeleaf, freemarker)์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ "UserForm" ์„ ๋ฆฌํ„ดํ•˜๊ฒŒ ๋˜๋ฉด resuource/templates ๋ฐ‘์— UserForm.html์„ ์ฐพ์•„ ๋žœ๋”๋ง ํ•ด์ค€๋‹ค.
  • ์•„๋ž˜ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด templates/response/hello.html์ด ๋žœ๋”๋ง ๋˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.
@RequestMapping("/response-view-v2")
public String responseViewV2(Model model) {    
    return "response/hello";
}

๐Ÿ”Ž ModelAndView

ModelAndView๊ฐ์ฒด๋Š” ๋žœ๋”๋งํ•˜๊ณ ์ž ํ•˜๋Š” View์˜ ๋…ผ๋ฆฌ์  ์ด๋ฆ„๊ณผ ํ•ด๋‹น View์—์„œ ์‚ฌ์šฉํ•  Model์„ ํ•จ๊ป˜ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

  • ModelAndView ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์ƒ์„ฑ์ž์— templatesํด๋” ์ดํ•˜์˜ ๊ฒฝ๋กœ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.
    • View Resolver๊ฐ€ prefix๋กœ templates๊นŒ์ง€์˜ ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.
  • ModelAndView์˜ addObject๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด key-value ์Œ์œผ๋กœ View๋‹จ์—์„œ ์‚ฌ์šฉํ•  Model์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.
  • ์ƒ์„ฑํ•œ ModelAndView๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ (ํ•ด๋‹น url๋กœ ์š”์ฒญ์ด ๋“ค์–ด์˜ฌ ๋•Œ) ์„ค์ •ํ•œ View๋ฅผ ๋žœ๋”๋งํ•˜๊ณ  View์—์„œ Model์„ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋„๋ก ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค.
@RequestMapping("/response-view-v1")
public ModelAndView responseViewV1() {
    ModelAndView modelAndView = new ModelAndView("response/hello").addObject("data", "test");
    return modelAndView;
}

๐Ÿ”Ž Response

String์„ ๊ทธ๋Œ€๋กœ View์— ์ถœ๋ ฅํ•  ๋•Œ ์ฃผ์˜

  • ์•„๋ž˜ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋ฌธ์ œ์—†์ด String๊ฐ’ "ok"๊ฐ€ ํ™”๋ฉด์— ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
@ResponseBody
@RequestMapping("/response-body-string-v3")
public String responseBodyStringV3() {
    return "ok";
 }
  • ์•„๋ž˜ ๋ฉ”์„œ๋“œ์˜ ๊ฒฝ์šฐ์—๋Š” 500 error๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.
    @RequestMapping("/response-body-string-v3")
    public String responseBodyStringV3() {
        return "ok";
    }
  • @ResponseBody์˜ ์œ ๋ฌด ๋•Œ๋ฌธ์ธ๋ฐ์š”. @ResponseBody๊ฐ€ ์žˆ๋‹ค๋ฉด ๋ฆฌํ„ด๋˜๋Š” ๊ฐ’์ด String์ด๋”๋ผ๋„ View Resolver๊ฐ€ ๋™์ž‘ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด์ง€์š”.
  • @ResponseBody๊ฐ€ ์—†๋‹ค๋ฉด ๋ฆฌํ„ด๋˜๋Š” String๊ฐ’์— ๋Œ€ํ•ด View Resolver๊ฐ€ ๋™์ž‘ํ•˜์—ฌ ํ•ด๋‹นํ•˜๋Š” View๋ฅผ ์ฐพ๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ResponseEntity๋ฅผ ์ด์šฉํ•œ Response - 1

HttpEntity๋ฅผ ์ƒ์†ํ•˜๋Š” ResponseEntity๋ฅผ ์ด์šฉํ•ด ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ตํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

  • ๊ฐ„๋‹จํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ƒ์„ฑํ•œ ๊ฐ์ฒด๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ตํ•˜๋Š” ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค.
  • ResponseEntity๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ƒํ™ฉ์— ๋งž๋Š” HttpStatus code๋„ ์‘๋‹ต ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • @ResponseBody ์ƒ๋žตํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค ! !
@ResponseBody
@RequestMapping("/response-body-json-v1")
public ResponseEntity<UserDto> responseBodyJsonV1() {
    UserDto userDto = new UserDto();
    userDto.setUsername("kim");
    userDto.setAge(26);

    return new ResponseEntity<>(userDto, HttpStatus.OK);
}

ResponseEntity๋ฅผ ์ด์šฉํ•œ Response - 2

๊ฐ์ฒด ์ž์ฒด๋ฅผ ๋ฆฌํ„ด ํƒ€์ž…์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

  • ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•˜๊ฒŒ ๋˜๋ฉด @ResponseBody์— ์˜ํ•ด ์•Œ์•„์„œ Http ์‘๋‹ต ๋ฉ”์‹œ์ง€์˜ Body์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • ResponseEntity๋ฅผ ์‚ฌ์šฉํ–ˆ๋˜ ๋ฐฉ๋ฒ•๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ HttpStatus code๋ฅผ ๋„ฃ๊ณ ์‹ถ๋‹ค๋ฉด @ResponseStatus๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
@ResponseStatus(HttpStatus.OK)
@ResponseBody
@RequestMapping("/response-body-json-v2")
public UserDto responseBodyJsonV2() {
    UserDto userDto = new UserDto();
    userDto.setUsername("kim");
    userDto.setAge(26);

    return userDto;
}

์ธํ”„๋Ÿฐ ๊น€์˜ํ•œ๋‹˜์˜ ์Šคํ”„๋ง MVC 1ํŽธ ์„ ์ˆ˜๊ฐ•ํ•˜๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

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