[Spring] @Controller, @RestController return

@Controller, @RestController return

@Controller์™€ @RestController ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ์ปจํŠธ๋กค๋Ÿฌ์˜ return์„ ์•Œ์•„๋ณด์ž.

@Controller

Rest๊ฐ€ ์—†๋Š” Controller๋กœ ์„ ์–ธํ•˜๊ณ  String ๊ฐ’์„ ๋ฆฌํ„ดํ•ด๋ณด์ž.

package com.vividswan.blog.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class UserController {

	@GetMapping("/user/loginForm")
	public String loginForm() {
		return "user/loginForm";
	}

}

ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ "/user/loginForm"์˜ ๊ฒฝ๋กœ๋กœ get ์š”์ฒญ์„ ๋ฐ›์•˜๊ณ , ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋ฅผ ์ „๋‹ฌํ•ด ์ค˜์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž.
get ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ํ•˜๊ธฐ ์œ„ํ•ด ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋งŒ๋“ค์–ด์•ผ ๋˜๊ณ , ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๊ฑฐ์นœ ๋’ค ํ•ด๋‹น ์ฃผ์†Œ์— ๋งž๋Š” ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋ฅผ reponse ํ•ด์•ผ ํ•œ๋‹ค.

์ด๋•Œ @Controller ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋งŒ๋“ค๊ณ , ๋งคํ•‘๋œ ๋ฉ”์„œ๋“œ๋กœ String ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋ฉด, view resolver๊ฐ€ return์— ๊ด€์—ฌํ•œ๋‹ค.

View Resolver

Controller์—์„œ return ๊ฐ’์œผ๋กœ String ์ž๋ฃŒํ˜•์„ return ํ•˜๋ฉด View Resolver๊ฐ€ String๊ณผ ๋งค์นญ๋˜๋Š” ํ™”๋ฉด์„ ์ฐพ์•„์„œ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „์†กํ•ด ์ค€๋‹ค.

์„ค์ •์ด ์•ˆ ๋“ค์–ด๊ฐ„ ๊ธฐ๋ณธ์ ์ธ View Resolver์˜ ๋งคํ•‘์€ templates/ + ๋ฐ˜ํ™˜ ๊ฐ’ + .html ํ˜•์‹์ด๋‹ค.

์ฆ‰, โ€œhelloโ€๋ผ๋Š” ๊ฐ’์„ return ํ•œ๋‹ค๋ฉด, templates ํด๋” ์•„๋ž˜์— hello.html์„ ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค.

View Resolver ๊ฒฝ๋กœ ๋ณ€๊ฒฝ

์Šคํ”„๋ง ๋ถ€ํŠธ์˜ application.yaml ๋“ฑ์˜ ์„ค์ • ํŒŒ์ผ์—์„œ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
jsp๋กœ ์‘๋‹ตํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

mvc:
  view:
    prefix: /WEB-INF/views/
    suffix: .jsp

์œ„์™€ ๊ฐ™์ด ์„ค์ •ํ•œ ๋’ค, ์ •ํ™•ํ•œ ์œ„์น˜์— return๊ฐ’.jsp ํŒŒ์ผ์ด ์žˆ๋‹ค๋ฉด, View Resolver๊ฐ€ ํ•ด๋‹น ํŒŒ์ผ์„ ํ™”๋ฉด์— ๋„์›Œ์ค„ ๊ฒƒ์ด๋‹ค.

@RestController

RestController๋Š” ๋‹จ์ˆœํ•œ String ๊ฐ’์„ ๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ๊ณ , ๊ฐ์ฒด๋ฅผ ๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ๋‹ค.

String return

package com.vividswan.blog.test;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BlogControllerTest {

	@GetMapping("/test/hello")
	public String hello() {
		return "<h1>hello spring boot</h1>";
	}
}

์œ„์™€ ๊ฐ™์ด ๋‹จ์ˆœํžˆ "<h1>hello spring boot</h1>"๋ฅผ return ํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋งŒ๋“ค๊ณ  ๋งคํ•‘๋œ ์ฃผ์†Œ์— get ์š”์ฒญ์„ ํ•ด๋ณด์ž.

1

์˜ฌ๋ฐ”๋ฅธ ๊ฒฝ๋กœ๋ผ๋ฉด String ๊ฐ’์„ return ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฐ์ฒด return

package com.vividswan.blog.test;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

// ์Šคํ”„๋ง์ด com.vividswan.blog ํŒจํ‚ค์ง€ ์ดํ•˜๋ฅผ ์Šค์บ”ํ•ด์„œ ๋ชจ๋“  ํŒŒ์ผ์„ ๋ฉ”๋ชจ๋ฆฌ์— new ํ•˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.
// ํŠน์ • ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์–ด์žˆ๋Š” ํด๋ž˜์Šค ํŒŒ์ผ๋“ค์„ newํ•ด์„œ(IoC) ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๊ด€๋ฆฌํ•ด์ค€๋‹ค.

@RestController
public class BlogControllerTest {

	//http://localhost:8080/test/hello
	@GetMapping("/test/hello")
	public Member hello() {
		Member member = new Member();
		member.setUsername("vividswan");
		member.setEmail("vividswan@naver.com");
		member.setPassword("12345");

		return member;
	}
}

์œ„ ์ปจํŠธ๋กค๋Ÿฌ์˜ hello ๋ฉ”์„œ๋“œ๋ฅผ ์‚ด์ง ๋ฐ”๊ฟ”์„œ, ๋ฉค๋ฒ„๋ผ๋Š” ๊ฐ์ฒด๋ฅผ return ํ•ด๋ณด์ž.

JSON์œผ๋กœ ๋ณ€ํ™˜ํ•ด ์ฃผ๋Š” MessageConverter

2

java object ํ˜•์‹์˜ ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋ผ JSON์œผ๋กœ ๋ฐ”๋€Œ์–ด์„œ ์ „์†ก๋˜์—ˆ๋‹ค!

@RestController์˜ ์–ด๋…ธํ…Œ์ด์…˜์ธ ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฉ”์„œ๋“œ์—์„œ ๊ฐ์ฒด ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค๋ฉด, MessageConverter์˜ jackson์ด java object๋ฅผ JSON ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ”์„œ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

์ด๋Ÿฌํ•œ ๋ฐฉ์‹์œผ๋กœ ํ”„๋ก ํŠธ๋‹จ์œผ๋กœ ์ž๋ฃŒ๋ฅผ ๋„˜๊ฒจ์ค„ ๋•Œ, JSON ํ˜•์‹์˜ ์ž๋ฃŒ๋ฅผ ๋ณด๋‚ด์ค„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ํ”„๋ก ํŠธ๋‹จ๋„ JSON ํ˜•์‹์˜ ์ž๋ฃŒ๋ฅผ ๊ฐ€๊ณตํ•˜์—ฌ ๊ทธ์— ๋งž๊ฒŒ ๊ฐœ๋ฐœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.