์ƒˆ์†Œ์‹

Back-End/Spring

[Spring] @Controller, @RestController ์ฐจ์ด์ 

  • -

 

 

 

Spring์˜ ์–ด๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜ MVC ํ”„๋ ˆ์ž„ ์›Œํฌ๋Š” RESTful ์›น ์„œ๋น„์Šค ์ƒ์„ฑ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋‹จ์ˆœํ™”ํ•ฉ๋‹ˆ๋‹ค. ์ „ํ†ต์ ์ธ Spring MVC ์ปจํŠธ๋กค๋Ÿฌ์™€ RESTful ์›น ์„œ๋น„์Šค ์ปจํŠธ๋กค๋Ÿฌ์˜ ์ฃผ์š” ์ฐจ์ด์ ์€ HTTP Response Body๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด MVC ์ปจํŠธ๋กค๋Ÿฌ๋Š” View ๊ธฐ์ˆ ์— ์˜์กดํ•˜์ง€๋งŒ RESTful ์›น ์„œ๋น„์Šค ์ปจํŠธ๋กค๋Ÿฌ๋Š” ๋‹จ์ˆœํžˆ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋Š” JSON / XML๋กœ HTTP ์‘๋‹ต์— ์ง์ ‘ ๊ธฐ๋ก๋ฉ๋‹ˆ๋‹ค.

 

 


@Controller(Spring MVC Controller)

1. Controller - View

์•„๋ž˜์™€ ๊ฐ™์€ ๊ณผ์ •์„ ํ†ตํ•ด Spring MVC Container๋Š” Client์˜ ์š”์ฒญ์œผ๋กœ๋ถ€ํ„ฐ View๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

Spring MVC ๊ธฐ์กด ์›Œํฌ ํ”Œ๋กœ

  • Client๋Š” URI ํ˜•์‹์œผ๋กœ ์›น ์„œ๋น„์Šค์— ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.
  • Mapping ๋˜๋Š” Handler์™€ ๊ทธ Type์„ ์ฐพ๋Š” DispatcherServlet์ด ์š”์ฒญ์„ ์ธํ„ฐ์…‰ํŠธํ•ฉ๋‹ˆ๋‹ค.
  • Controller๊ฐ€ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ ํ›„์— ์‘๋‹ต์„ DispatcherServlet์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ณ , DispatcherServlet์€ View๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

@Controller๊ฐ€ View๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ViewResolver๊ฐ€ ์‚ฌ์šฉ๋˜๋ฉฐ, ViewResolver ์„ค์ •์— ๋งž๊ฒŒ View๋ฅผ ์ฐพ์•„ ๋ Œ๋”๋ง ํ•ฉ๋‹ˆ๋‹ค. ๋˜๋Š” application.properties ๋“ฑ์„ ํ†ตํ•ด ๋ Œ๋”๋ง์„ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

spring.mvc.view.prefix=/WEB-INF/templates/
spring.mvc.view.suffix=.jsp

 

 

 

2. Controller - @ResponseBody

ํ•˜์ง€๋งŒ Spring MVC์˜ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ๋„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. Spring MVC์˜ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด ๋ฉ”์†Œ๋“œ์—์„œ @ResponseBody๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Spring์€ ๋ฐ˜ํ™˜ ๊ฐ’์„ ๋ณ€ํ™˜ํ•˜์—ฌ Json ํ˜•ํƒœ์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Spring 3.x MVC RESTful ์›น ์„œ๋น„์Šค ์›Œํฌ ํ”Œ๋กœ์šฐ

  • Client๋Š” URI ํ˜•์‹์œผ๋กœ ์›น ์„œ๋น„์Šค์— ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.
  • Mapping ๋˜๋Š” Handler์™€ ๊ทธ Type์„ ์ฐพ๋Š” DispatcherServlet์ด ์š”์ฒญ์„ ์ธํ„ฐ์…‰ํŠธํ•ฉ๋‹ˆ๋‹ค.
  • @ResponseBody๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Client์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

3. ์˜ˆ์ œ

Employee๋ผ๋Š” ๋‹ค์Œ Java ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด ํด๋ž˜์Šค๋Š” POJO์ž…๋‹ˆ๋‹ค.

import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement(name = "Employee")
public class Employee {
 
    String name;
 
    String email;
 
    public String getName() {
   return name;
    }
 
    public void setName(String name) {
   this.name = name;
    }
 
    public String getEmail() {
   return email;
    }
 
    public void setEmail(String email) {
   this.email = email;
    }
 
    public Employee() {
    }
 
}

๋‹ค์Œ @Controller ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.example.spring.model.Employee;
 
@Controller
@RequestMapping("employees")
public class EmployeeController {
 
    Employee employee = new Employee();
    
    @RequestMapping("employee")
    public String employee() {
        return "employee/index";
    }
 
    @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody Employee getEmployeeInJSON(@PathVariable String name) {
 
   employee.setName(name);
   employee.setEmail("employee1@genuitec.com");
 
   return employee;
 
    }
 
    @RequestMapping(value = "/{name}.xml", method = RequestMethod.GET, produces = "application/xml")
    public @ResponseBody Employee getEmployeeInXML(@PathVariable String name) {
 
   employee.setName(name);
   employee.setEmail("employee1@genuitec.com");
 
   return employee;
 
    }
 
}

employee ๋ฉ”์„œ๋“œ๋Š” ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด "employee/index" ๋ผ๋Š” ๊ฒฝ๋กœ์˜ view ํŒŒ์ผ์„ ์—ด๊ฒ ๋‹ค๊ณ  ์„ ์–ธํ•œ ์ „ํ†ต @Controller๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ๋‚˜๋จธ์ง€ ๋ฉ”์„œ๋“œ๋Š” @ResponseBody๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž‘์„ฑ๋œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

 

 

 

 

@RestController(Spring Restful Controller)

1. RestController

Spring 4.0์€ @Controller ๋ฐ @ResponseBody๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ์˜ ํŠน์ˆ˜ ๋ฒ„์ „์œผ๋กœ @RestController๋ฅผ ๋„์ž…ํ–ˆ์Šต๋‹ˆ๋‹ค. @RestController ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค์— ์–ด๋…ธํ…Œ์ด์…˜์„ ์ž‘์„ฑํ•˜๋ฉด ๋” ์ด์ƒ ๋ชจ๋“  ์š”์ฒญ ๋งตํ•‘ ๋ฉ”์„œ๋“œ์— @ResponseBody๋ฅผ ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. @ResponseBody๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ™œ์„ฑํ™”์‹œ์ผœ์ฃผ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

Spring 4.x MVC RESTful ์›น ์„œ๋น„์Šค ์›Œํฌ ํ”Œ๋กœ์šฐ

  1. Client๋Š” URI ํ˜•์‹์œผ๋กœ ์›น ์„œ๋น„์Šค์— ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.
  2. Mapping ๋˜๋Š” Handler์™€ ๊ทธ Type์„ ์ฐพ๋Š” DispatcherServlet์ด ์š”์ฒญ์„ ์ธํ„ฐ์…‰ํŠธํ•ฉ๋‹ˆ๋‹ค.
  3. RestController๋Š” ํ•ด๋‹น ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

2. ์˜ˆ์ œ

์˜ˆ์ œ์—์„œ @RestController๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด @Controller๋ฅผ @RestController๋กœ ์ˆ˜์ •ํ•˜๊ณ  ๊ฐ ๋ฉ”์„œ๋“œ์—์„œ @ResponseBody๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ ํด๋ž˜์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import com.example.spring.model.Employee;
 
@RestController
@RequestMapping("employees")
public class EmployeeController {
 
    Employee employee = new Employee();
 
    @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json")
    public Employee getEmployeeInJSON(@PathVariable String name) {
 
   employee.setName(name);
   employee.setEmail("employee1@genuitec.com");
 
   return employee;
 
    }
 
    @RequestMapping(value = "/{name}.xml", method = RequestMethod.GET, produces = "application/xml")
    public Employee getEmployeeInXML(@PathVariable String name) {
 
   employee.setName(name);
   employee.setEmail("employee1@genuitec.com");
 
   return employee;
 
    }
 
}

๋” ์ด์ƒ @ResponseBody๋ฅผ ์š”์ฒญ ๋งคํ•‘ ๋ฉ”์„œ๋“œ์— ์ถ”๊ฐ€ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๋ณ€๊ฒฝ ํ•œ ํ›„ ์„œ๋ฒ„์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋‹ค์‹œ ์‹คํ–‰ํ•˜๋ฉด ์ด์ „๊ณผ ๋™์ผํ•œ ์ถœ๋ ฅ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

 

์œ„์™€ ๊ฐ™์ด @RestController๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋ฉฐ Spring v4.0๋ถ€ํ„ฐ MVC RESTful ์›น ์„œ๋น„์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์„ ํ˜ธ๋˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

 

 

์ถœ์ฒ˜๋Š” ์—ฌ๊ธฐ๋ฅผ ํด๋ฆญํ•ด ์ฃผ์„ธ์š”.

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜Š

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๐Ÿ‘