Home > Spring > ๐Ÿƒ[Spring] ๊ณ„์ธตํ˜• ์•„ํ‚คํ…์ฒ˜์—์„œ Controller์˜ ์—ญํ• .

๐Ÿƒ[Spring] ๊ณ„์ธตํ˜• ์•„ํ‚คํ…์ฒ˜์—์„œ Controller์˜ ์—ญํ• .
Spring Framework

๐Ÿƒ[Spring] ๊ณ„์ธตํ˜• ์•„ํ‚คํ…์ฒ˜์—์„œ Controller์˜ ์—ญํ• .

Java ๋ฐฑ์•ค๋“œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ณ„์ธตํ˜• ์•„ํ‚คํ…์ฒ˜(Layerd Architecture) ์—์„œ Controller๋Š” ์‚ฌ์šฉ์ž(ํด๋ผ์ด์–ธํŠธ)๋กœ๋ถ€ํ„ฐ ์š”์ฒญ์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๊ณ , ๊ทธ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๋Š” ์ค‘์š”ํ•œ ๊ณ„์ธต์ž…๋‹ˆ๋‹ค.

Controller๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์™ธ๋ถ€ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ž‘๋™ํ•˜๋ฉฐ, ์ฃผ๋กœ ์›น ์š”์ฒญ๊ณผ ์‘๋‹ต์˜ ํ๋ฆ„์„ ์ œ์–ดํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

1๏ธโƒฃ Controller์˜ ์ฃผ์š” ์—ญํ• .

1. ์š”์ฒญ ์ˆ˜์‹  ๋ฐ ์‘๋‹ต ๋ฐ˜ํ™˜.

  • Controller๋Š” ํด๋ผ์ด์–ธํŠธ(๋ธŒ๋ผ์šฐ์ €, ๋ชจ๋ฐ”์ผ ์•ฑ ๋“ฑ)๋กœ ๋ถ€ํ„ฐ HTTP ์š”์ฒญ์„ ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ ์š”์ฒญ์€ URL ๊ฒฝ๋กœ ๋ฐ HTTP ๋ฉ”์„œ๋“œ(GET, POST, PUT, DELETE ๋“ฑ)์— ๋”ฐ๋ผ Controller์˜ ํŠน์ • ๋ฉ”์„œ๋“œ์— ๋งคํ•‘๋ฉ๋‹ˆ๋‹ค.
  • ์ด ๋ฉ”์„œ๋“œ๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ํ˜ธ์ถœํ•˜๊ณ , ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ต์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

2. ์š”์ฒญ ๊ฒ€์ฆ.

  • Controller๋Š” ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์ „๋‹ฌ๋œ ์š”์ฒญ ๋ฐ์ดํ„ฐ(์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ, ํผ ๋ฐ์ดํ„ฐ, JSON ๋“ฑ)๋ฅผ ๊ฒ€์ฆํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
  • ์ด ๊ฒ€์ฆ ์ž‘์—…์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์š”์ฒญ์ด ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์œผ๋กœ ์ „๋‹ฌ๋˜๊ธฐ ์ „์— ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค.
  • ๊ฒ€์ฆ ์‹คํŒจ ์‹œ, ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋‚˜ ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

3. ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ํ˜ธ์ถœ.

  • Controller๋Š” ์ง์ ‘์ ์œผ๋กœ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๊ณ , Service ๊ณ„์ธต์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
  • ์ด ๋ฐฉ์‹์€ ์—ญํ• ์„ ๋ถ„๋ฆฌํ•˜์—ฌ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ๋†’์ด๊ณ , ์ฝ”๋“œ๊ฐ€ ๋”์šฑ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
  • Service ๊ณ„์ธต์ด ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด, Controller๋Š” ์ด๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

4. ๋ทฐ์™€์˜ ํ†ต์‹ (View์™€ ์—ฐ๋™)

  • Controller๋Š” View์™€ ํ†ต์‹ ํ•˜์—ฌ ์ ์ ˆํ•œ ์‘๋‹ต์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” HTML์ด๋‚˜ JSON ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค.
  • ์˜ˆ๋ฅผ ๋“ค์–ด, ํ…œํ”Œ๋ฆฟ ์—”์ง„(Thymeleaf ๋“ฑ)์„ ์‚ฌ์šฉํ•ด ๋™์ ์ธ HTML ํŽ˜์ด์ง€๋ฅผ ๋ Œ๋”๋งํ•˜๊ฑฐ๋‚˜, API ์„œ๋ฒ„์˜ ๊ฒฝ์šฐ JSON ํฌ๋งท์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

5. HTTP ์ƒํƒœ ์ฝ”๋“œ ๊ด€๋ฆฌ.

  • Controller๋Š” ์š”์ฒญ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ ์ ์ ˆํ•œ HTTP ์ƒํƒœ ์ฝ”๋“œ(์˜ˆ: 200 OK, 400 Bad Request, 404 Not Found, 500 Internal Server Error ๋“ฑ) ๋ฅผ ์„ค์ •ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  • ์ด๋กœ์จ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญ ์ฒ˜๋ฆฌ ์ƒํƒœ๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

6. ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

  • Controller๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ฑฐ๋‚˜, ์ „์—ญ์ ์ธ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์‚ฌ์šฉํ•ด ์˜ˆ์™ธ๋ฅผ ๋‹ค๋ฃน๋‹ˆ๋‹ค.
  • ์ด๋ฅผ ํ†ตํ•ด ์ ์ ˆํ•œ ์—๋Ÿฌ ์‘๋‹ต์„ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋น„์ •์ƒ์ ์œผ๋กœ ์ข…๋ฃŒ๋˜์ง€ ์•Š๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

2๏ธโƒฃ ์˜ˆ์‹œ ์ฝ”๋“œ

Spring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ์˜ Controller ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด ๊ทธ ์—ญํ• ์„ ๋” ๊ตฌ์ฒด์ ์œผ๋กœ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@RestController
@RequestMapping("/users")
public class UserController {
    
    private final UserService userService;
    
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<UserDTO> getUser(@PathVariable Long id) {
        UserDTO user = userService.getUserById(id);
        return ResponseEntity.ok(user);
    }
    
    @PostMapping
    public ResponseEntity<UserDTO> createUser(@RequestBody @Valid UserDTO userDTO) {
        UserDTO createdUser = userService.createUser(userDTO);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdUser);
    }
}

3๏ธโƒฃ ์„ค๋ช….

  • @RestController
    • Spring์—์„œ RESTful ์›น ์„œ๋น„์Šค๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ž…๋‹ˆ๋‹ค.
    • JSON ๋˜๋Š” XML ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
  • @RequestMapping
    • ์ด ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ \users ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • @GetMapping
    • HTTP GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, id ๊ฒฝ๋กœ ๋ณ€์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์šฉ์ž๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  • @PostMapping
    • HTTP POST ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, ์ƒˆ๋กœ์šด ์‚ฌ์šฉ์ž๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • @RequestBody, @Valid
    • ์š”์ฒญ ๋ณธ๋ฌธ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒด๋กœ ๋งคํ•‘ํ•˜๊ณ , ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.
  • ResponseEntity
    • ์‘๋‹ต ๋ณธ๋ฌธ๊ณผ ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ํฌํ•จํ•ด ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

4๏ธโƒฃ ๊ฒฐ๋ก 

Contoller๋Š” ์‚ฌ์šฉ์ž ์š”์ฒญ์„ ๋ฐ›์•„ Service ๊ณ„์ธต๊ณผ ํ†ต์‹ ํ•˜๋ฉฐ, ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
์ด๋Ÿฌํ•œ ์—ญํ•  ๋ถ„๋ฆฌ๋ฅผ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ๋”์šฑ ์œ ์—ฐํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ์‰ฌ์šด ๊ตฌ์กฐ๋ฅผ ๊ฐ–์ถ”๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.