[ SpringMVC์— ๋Œ€ํ•˜์—ฌ ]

 

Spring - Spring MVC๋ฅผ ์•Œ์•„์•ผ ์‹œ์ž‘์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์ฒ˜์Œ์—” ๋Œ€์ถฉ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•ด๋ณด๋ คํ–ˆ์ง€๋งŒ,์ด๋Ÿด์ˆ˜๊ฐ€! ์•„๋Š” ๊ฒƒ์ด ๋ฏธ์•ฝํ•˜๋‹ˆ ์‹œ์ž‘ํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.๊ทธ๋ž˜์„œ ๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์œผ๋กœ ์•Œ์•„์•ผํ•˜๋Š” ๊ฐœ๋…์„ ์ฐจ๊ทผ์ฐจ๊ทผ ์ •๋ฆฌํ•ด๋ณผ๊นŒ ํ•œ๋‹ค.๋‘๋‘ฅ, Spring MVC๋ž€?์•„๋งˆ ์ •๋ณด์ฒ˜

post-this.tistory.com

 

 

 

 

[ Hello World ์ถœ๋ ฅํ•˜๊ธฐ ]

 

Spring - [IntelliJ] Hello World ์ถœ๋ ฅํ•˜๊ธฐ

[ Spring MVC์— ๋Œ€ํ•˜์—ฌ ] Spring - Spring MVC๋ฅผ ์•Œ์•„์•ผ ์‹œ์ž‘์ด ๊ฐ€๋Šฅํ•˜๋‹ค.์ฒ˜์Œ์—” ๋Œ€์ถฉ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•ด๋ณด๋ คํ–ˆ์ง€๋งŒ,์ด๋Ÿด์ˆ˜๊ฐ€! ์•„๋Š” ๊ฒƒ์ด ๋ฏธ์•ฝํ•˜๋‹ˆ ์‹œ์ž‘ํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.๊ทธ๋ž˜์„œ ๊ฐ€์žฅ ๊ธฐ์ดˆ์ ์œผ๋กœ ์•Œ์•„์•ผํ•˜๋Š”

post-this.tistory.com

 


 

 

Hello, World๋ฅผ ์ถœ๋ ฅํ–ˆ๋‹ค.
๊ทธ๋Ÿฐ๋ฐ @Controller๊ฐ€ ์•„๋‹Œ @RestController๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

์™œ @RestController๋ฅผ ์‚ฌ์šฉํ•œ๊ฑธ๊นŒ?

 

 

 

 

@Controller

  • @Controller๋Š” ์ฃผ๋กœ view๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ViewResolver๋ฅผ ํ†ตํ•ด view๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • AnnotationMethodHandlerAdapter ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ๊ฐ€ ์‚ฌ์šฉํ•œ๋‹ค.
  • ๋ฉ”์†Œ๋“œ ๋‹จ์œ„๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํšจ์œจ์ด ์ข‹๋‹ค.
  • @Controller๋Š” 200 OK(์„ฑ๊ณต์„ ์•Œ๋ฆฌ๋Š” ์ƒํƒœ ์ฝ”๋“œ)๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณด๋‚ธ๋‹ค.

 

 

@RestController

  • @Controller์— @ResponseBody๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.
  • Controller๋ฅผ ํ†ตํ•ด Json ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.
  • HttpMessageConverter๋ฅผ ํ†ตํ•ด Json ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ์ฒด๋Š” Json์œผ๋กœ Serialize(์ง๋ ฌํ™”)๋˜์–ด ๋ฐ˜ํ™˜๋œ๋‹ค.
  • @RestController๋Š” 200 OK(์„ฑ๊ณต์„ ์•Œ๋ฆฌ๋Š” ์ƒํƒœ ์ฝ”๋“œ)๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ณด๋‚ธ๋‹ค.

 

 

 

๊ฒฐ๋ก ์ ์œผ๋กœ @Controller๋Š” View๋ฅผ ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๊ณ , @RestController๋Š” Hello, World ๊ฐ™์€ ์–ด๋–ค Json ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๊ฑธ ์•Œ์•˜๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ์™œ @RestController๋Š” Json ๋ฐ˜ํ™˜์ด ๋˜๋Š”๊ฑธ๊นŒ?

 

 

 

 

์ง๋ ฌํ™”

  • ์ปดํ“จํ„ฐ ๊ณผํ•™์˜ ๋ฐ์ดํ„ฐ ์Šคํ† ๋ฆฌ์ง€ ๋ฌธ๋งฅ์—์„œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋‚˜ ์˜ค๋ธŒ์ ํŠธ ์ƒํƒœ๋ฅผ ๋™์ผํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ ํ™˜๊ฒฝ์— ์ €์žฅํ•˜๊ณ  ๋‚˜์ค‘์— ์žฌ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ํฌ๋งท์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.
  • ์ž๋ฐ” ์‹œ์Šคํ…œ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์™ธ๋ถ€์˜ ์ž๋ฐ” ์‹œ์Šคํ…œ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐ”์ดํŠธ(byte) ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ์ˆ ๊ณผ ๋ฐ”์ดํŠธ๋กœ ๋ณ€ํ™˜๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ธฐ์ˆ (์—ญ์ง๋ ฌํ™”)์„ ์•„์šธ๋Ÿฌ์„œ ์ด์•ผ๊ธฐํ•œ๋‹ค.

 

 

 

์ง๋ ฌํ™”๋ฅผ ์‰ฝ๊ฒŒ ์ดํ•ดํ•ด๋ณด์ž๋ฉด, ์‹œ์Šคํ…œ ๋‚ด๋ถ€์—์„œ ์ฒ˜๋ฆฌํ•˜๊ธฐ ํŽธ๋ฆฌํ•œ ์–ด๋–ค ๋ฐ์ดํ„ฐ ํ˜•ํƒœ๋กœ ์กด์žฌํ•˜๋‹ค๊ฐ€ ์šฐ๋ฆฌ์—๊ฒŒ ๋ณด์—ฌ์ค„ ๋•Œ ์šฐ๋ฆฌ๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ์Šต์œผ๋กœ ํ˜•ํƒœ๋ฅผ ๋ณ€ํ™”์‹œํ‚ค๋Š” ๊ฒƒ์„ ๋œปํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

 

 

 

 

 

Q. @Controller๋Š” Json ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜์„ ๋ชปํ•˜๋‚˜?

 

A. ๊ทธ๊ฑด ์•„๋‹ˆ๋‹ค. ํ•  ์ˆ˜ ์žˆ๋‹ค. @ResponseBody๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด Json ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ณธ๋ž˜ @Controller๋Š” ViewResolver๊ฐ€ ์ž‘๋™ํ•˜์—ฌ view๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค. ํ•˜์ง€๋งŒ @ResponseBody๋ฅผ ๋ถ™์ด๋ฉด @RestController์ฒ˜๋Ÿผ HttpMessageConverter๊ฐ€ ๊ฐœ์ž…ํ•˜๊ฒŒ ๋˜์–ด ๊ฐ์ฒด๋ฅผ ์ง๋ ฌํ™”ํ•˜์—ฌ ์ „๋‹ฌํ•˜๊ฒŒ ๋œ๋‹ค. ๋˜ํ•œ MessageConverter์—๋Š” ๋ฐ์ดํ„ฐ์˜ ์ข…๋ฅ˜(๋‹จ์ˆœ ๋ฌธ์ž์—ด์ธ์ง€, ๊ฐ์ฒด์ธ์ง€...)์— ๋”ฐ๋ผ ์„œ๋กœ ๋‹ค๋ฅธMessageConverter๊ฐ€ ์ž‘๋™ํ•˜๊ฒŒ ๋œ๋‹ค.

// @ResponseBody๋ฅผ ๋ถ™์ธ ์ฝ”๋“œ
@Controller
public class HelloController {

    @GetMapping("")
    @ResponseBody
    public String helloWorld() {
        return "Hello, World";
    }

}

 

<๊ฒฐ๊ณผ>

๐Ÿ‘

 

 

 


 

 

 

 

Http ์ƒํƒœ ์ฝ”๋“œ

  • ํŠน์ • HTTP ์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋˜์—ˆ๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค.
  • ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ์„ฑ๊ณต์‘๋‹ต์€ 200 OK๊ฐ€ ์žˆ๋‹ค.

 

- ๋” ๋‹ค์–‘ํ•œ ์ƒํƒœ ์ฝ”๋“œ

 

HTTP ์ƒํƒœ ์ฝ”๋“œ - HTTP | MDN

HTTP ์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ๋Š” ํŠน์ • HTTP ์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋˜์—ˆ๋Š”์ง€ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ์‘๋‹ต์€ 5๊ฐœ์˜ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง‘๋‹ˆ๋‹ค: ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์‘๋‹ต, ์„ฑ๊ณต์ ์ธ ์‘๋‹ต, ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ, ํด๋ผ์ด์–ธํŠธ ์—๋Ÿฌ, ๊ทธ๋ฆฌ๊ณ 

developer.mozilla.org

 

 

 

 

Q. @RestController์˜ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ์„ฑ๊ณต ์ƒํƒœ ์ฝ”๋“œ๋Š” 200 OK๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋‹ค๋ฅธ ์ƒํƒœ ์ฝ”๋“œ๋กœ ๋ฐ”๊พธ๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•˜๋‚˜?

A. ResponseEntity๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

1) ResponseEntity.ok()๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ (200 OK)

// ์˜ˆ์ œ ์ถœ์ฒ˜ - mangkyu.tistory (๋ง๋‚˜๋‹ˆ ๊ฐœ๋ฐœ์ž๋‹˜)

  @GetMapping(value = "/users")
    public ResponseEntity<User> findUserWithResponseEntity(@RequestParam String userName){
        return ResponseEntity.ok(userService.findUser(userName));
    }

 

 

2) ResponseEntity.status(HttpStatus.CREATED)๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ (201 Created)

@GetMapping(value = "/users")
public ResponseEntity<User> findUserWithCreatedResponse(@RequestParam String userName){
    return ResponseEntity.status(HttpStatus.CREATED).body(userService.findUser(userName));
}

 

์ฝ”๋“œ ์„ค๋ช… :

  1. @GetMapping โ–ป users ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์„ ํ•ด๊ฒฐํ•˜๊ณ  ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  2. ResponseEntity<User> โ–ป User(User ์•ˆ์— ์žˆ์„ ์–ด๋–ค ์ •๋ณด๋“ค, ์ด๋ฆ„+๋‚˜์ด+์ฃผ์†Œ์™€ ๊ฐ™์€...) ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, ์ƒํƒœ ์ฝ”๋“œ์™€ ํ•จ๊ป˜ ๋ฐ˜ํ™˜ํ•˜๋‹ค๋Š” ๋œป. (ResponseEntity๋กœ ๊ฐ์‹ธ์„œ ๋ฐ˜ํ™˜)
  3. @RequestParam  โ–ป ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ๋ฐ”์ธ๋”ฉ(๊ตฌ์ฒด์ ์ธ ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๊ณผ์ •, ์ด๋ฆ„ = ๋ฏธ๋‚˜ + ๋‚˜์ด = 10์„ธ + ์ฃผ์†Œ = ์„œ์šธํŠน๋ณ„์‹œ์™€ ๊ฐ™์€...) ๊ทธ๋Ÿฐ๋ฐ userName์ด ์˜†์— ๋ถ™์—ˆ๋‹ค. ์ด ์˜๋ฏธ๋Š” ๊ตฌ์ฒด์ ์ธ ๊ฐ’์œผ๋กœ userName์ด ๋“ค์–ด์˜ค๋ฉด(์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ) userName ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๊ฒ ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. (๋ฏธ๋‚˜๋ผ๋Š” ๊ฐ’์ด url๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์˜ค๋ฉด ๊ทธ ๊ฐ’์„ userName์— ์ €์žฅํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๊ทธ ์‚ฌ์šฉ์ž์— ๋Œ€ํ•œ ๋‹ค๋ฅธ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€์ ์œผ๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ ๋‹ค๋Š” ์˜๋ฏธ์ธ ๊ฒƒ ๊ฐ™๋‹ค.)
  4. ResponseEntity.status  โ–ป ์‘๋‹ต์ƒํƒœ ์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•˜๋„๋ก ํ•œ๋‹ค.
  5. HttpStatus.CREATED  โ–ป 201 Created๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์—ˆ์œผ๋ฉฐ, ์ž์›์ด ์ƒ์„ฑ๋˜์—ˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์„ฑ๊ณต ์ƒํƒœ ์‘๋‹ต ์ฝ”๋“œ์ด๋‹ค. 
  6. findUser(userName)  โ–ป userName์„ ์ฐพ์•„์„œ User ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ๋Š” ๊ฒƒ์ด๋‹ค.

 

๊ฒฐ๋ก  : users๋กœ ๋“ค์–ด์˜จ ์š”์ฒญ์„ ํ•ด๊ฒฐํ•œ ํ›„ ์„ฑ๊ณต์ ์ด๋ฉด 201 CREATED๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ๋ฐ˜ํ™œํ•  ๋•Œ User ๋‚ด์šฉ๋„ ๊ฐ™์ด ๋‹ด์•„์„œ ๋ฐ˜ํ™˜ํ•ด๋ผ. 

 

 

 

 

 

 

Q. ๊ทธ๋ ‡๋‹ค๋ฉด @Controller๋Š” ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€?

 

A. @RestController์™€ ๊ฐ™์ด ResponseEntity.status๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ, HttpServletResponse ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด  HttpServletResponse response์™€ response.setStatus(HttpStatus.CREATED.value()); ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด 201 CREATED๋กœ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

    @GetMapping("/users")
    public String findUser(@RequestParam String userName, HttpServletResponse response) {        
        response.setStatus(HttpStatus.CREATED.value());
    }

 

 

 


 

 

์ด๋ฒˆ์—๋Š” @Controller์™€ @RestController์— ๋Œ€ํ•ด ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค.

์ƒ๊ฐ๋ณด๋‹ค ์ •๋ฆฌํ•˜๋Š”๋ฐ ์˜ค๋ž˜๊ฑธ๋ ค์„œ ์กฐ๊ธˆ ๋‹นํ™ฉ์Šค๋Ÿฌ์› ๋„ค์š”...

 

๋‹ค ์ฒ˜์Œ๋ณด๋Š” ๋‚ด์šฉ๋“ค์ด๋ผ ํ•˜๋‚˜์˜ ๊ฐœ๋…์„ ์ดํ•ดํ•˜๋ ค๋ฉด ๋‹ค๋ฅธ ๊ฐœ๋…๋“ค๋„ ์ดํ•ดํ•ด์•ผํ–ˆ๊ณ 
์ƒํƒœ ์ฝ”๋“œ์— ๋Œ€ํ•œ ๋ถ€๋ถ„๋„ ๋ชฐ๋ž๋‹ค๊ฐ€ ์–ผ๋–จ๊ฒฐ์— ๊ฐ„๋‹จํžˆ ๋ฐฐ์šฐ๊ณ  ๋„˜์–ด๊ฐ€๊ฒŒ ๋˜๋Š” ์‹œ๊ฐ„์ด์—ˆ์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜์— ์ถœ์ฒ˜๋ฅผ ๋‚จ๊ฒจ๋‘๊ฒ ์Šต๋‹ˆ๋‹ค.
๋ธ”๋กœ๊ทธ์—” ์ œ๊ฐ€ ์ตœ๋Œ€ํ•œ ์ดํ•ดํ•œ ๋‚ด์šฉ๊นŒ์ง€๋งŒ ์ ๋Š”๊ฑฐ๋ผ
์ถœ์ฒ˜์— ๋“ค์–ด๊ฐ€์‹œ๋ฉด ๋” ๋งŽ์€ ๋‚ด์šฉ์ด ์žˆ์–ด ๊ณต๋ถ€ํ•˜์‹œ๋Š”๋ฐ ๋„์›€๋˜์‹ค๊ฒ๋‹ˆ๋‹ค.

 

๐Ÿชฝ

ํ‹€๋ฆฐ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”

 

 


 

 

- @Controller์™€ @RestController์˜ ์›๋ฆฌ์™€ ์ฐจ์ด ๊ทธ๋ฆฌ๊ณ  ์ƒํƒœ์ฝ”๋“œ

 

[Spring] @Controller์™€ @RestController ์ฐจ์ด

Spring์—์„œ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ง€์ •ํ•ด์ฃผ๊ธฐ ์œ„ํ•œ ์–ด๋…ธํ…Œ์ด์…˜์€ @Controller์™€ @RestController๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ „ํ†ต์ ์ธ Spring MVC์˜ ์ปจํŠธ๋กค๋Ÿฌ์ธ @Controller์™€ Restuful ์›น์„œ๋น„์Šค์˜ ์ปจํŠธ๋กค๋Ÿฌ์ธ @RestController์˜ ์ฃผ์š”ํ•œ ์ฐจ์ด์ 

mangkyu.tistory.com

 

 

 

- ๋‹ค์–‘ํ•œ Controller์— ๋Œ€ํ•ด์„œ

 

[Spring] Controller์˜ ์ข…๋ฅ˜์™€ ๋‹ค์–‘ํ•œ ์ „๋žต

1. Controller์˜ ์ข…๋ฅ˜ 1) Servlet - javax.servlet.Servlet์„ ์ƒ์†ํ•œ ํด๋ž˜์Šค๋ฅผ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. @Component("/home") // bean ์ด๋ฆ„์„ ์ด์šฉํ•œ ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘ ๋ฐฉ์‹ ์ด์šฉ public class HomeServlet extends HttpServlet { protected void

kimcoder.tistory.com

 

 

 

- ์ง๋ ฌํ™”์— ๋Œ€ํ•ด์„œ

 

์ง๋ ฌํ™” - ์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ๋ฐฑ๊ณผ์‚ฌ์ „

์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ๋ฐฑ๊ณผ์‚ฌ์ „. ์ง๋ ฌํ™”(็›ดๅˆ—ๅŒ–) ๋˜๋Š” ์‹œ๋ฆฌ์–ผ๋ผ์ด์ œ์ด์…˜(serialization)์€ ์ปดํ“จํ„ฐ ๊ณผํ•™์˜ ๋ฐ์ดํ„ฐ ์Šคํ† ๋ฆฌ์ง€ ๋ฌธ๋งฅ์—์„œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋‚˜ ์˜ค๋ธŒ์ ํŠธ ์ƒํƒœ๋ฅผ ๋™์ผํ•˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ ํ™˜๊ฒฝ

ko.wikipedia.org

 

 

 

 

- @RequestParam์— ๋Œ€ํ•ด์„œ

 

๐ŸŒฑ @RequestParam ๊ฐœ๋… ์ •๋ฆฌ

@RequestParam ์Šคํ”„๋ง์ด ์ œ๊ณตํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ, ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋งค์šฐ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค @ResponseBody @RequestMapping("/request-param") public String requestParamV2( @RequestParam("username") Strin

yummy0102.tistory.com

 

 

 

- HttpservletResponse์— ๋Œ€ํ•ด์„œ

 

์„œ๋ธ”๋ฆฟ(Servlet) - HttpservletResponse ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•, HTTP ์‘๋‹ต ๋ฐ์ดํ„ฐ ๋‹จ์ˆœ ํ…์ŠคํŠธ & HTML & API JSON

์ธํ”„๋Ÿฐ ๊น€์˜ํ•œ ๋‹˜์˜ ์Šคํ”„๋ง ๊ฐ•์˜์ด๋ฉฐ, ์„น์…˜ 2 - ์„œ๋ธ”๋ฆฟ(Servlet)๋ฅผ ์ •๋ฆฌํ•œ๋‹ค. ์‹ค์Šตํ˜• ๊ต์œก์œผ๋กœ ์ž์„ธํ•œ ์„ค๋ช…์ด ๊ถ๊ธˆํ•˜๋ฉด ์ˆ˜๊ฐ•์„ ๊ถŒ์žฅํ•œ๋‹ค. (์‹ค์Šต์€ ๋ชจ๋‘ ์˜›๋‚  ๋ฐฉ์‹์ด๋ฉฐ, ํ˜„์žฌ๋Š” ์ข€ ๋” ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•œ

loopstudy.tistory.com

 

 

 

- ๋‹ค์–‘ํ•œ ์ƒํƒœ ์ฝ”๋“œ

 

HTTP ์ƒํƒœ ์ฝ”๋“œ - HTTP | MDN

HTTP ์‘๋‹ต ์ƒํƒœ ์ฝ”๋“œ๋Š” ํŠน์ • HTTP ์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋˜์—ˆ๋Š”์ง€ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ์‘๋‹ต์€ 5๊ฐœ์˜ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋ˆ„์–ด์ง‘๋‹ˆ๋‹ค: ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์‘๋‹ต, ์„ฑ๊ณต์ ์ธ ์‘๋‹ต, ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ, ํด๋ผ์ด์–ธํŠธ ์—๋Ÿฌ, ๊ทธ๋ฆฌ๊ณ 

developer.mozilla.org

 

 

 

- ์Šคํ”„๋ง์‚ฌ์ „

 

ResponseBody (Spring Framework 6.1.14 API)

Annotation that indicates a method return value should be bound to the web response body. Supported for annotated handler methods. As of version 4.0 this annotation can also be added on the type level in which case it is inherited and does not need to be a

docs.spring.io

 

 

 

 

- @ResponseStatus์— ๋Œ€ํ•˜์—ฌ

 

Spring์—์„œ ํŠน์ • HTTP ์‘๋‹ต ์ƒํƒœ ๋ฐ˜ํ™˜ํ•˜๊ธฐ

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ํŠน์ • HTTP ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ์„œ๋ฒ„๋Š” HTTP ์‘๋‹ต ์ƒํƒœ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํด๋ผ์ด์–ธํŠธ๋Š” ์‘๋‹ต ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด์„œ ์„œ๋ฒ„๊ฐ€ ์š”์ฒญ ์ฒ˜๋ฆฌ๋ฅผ ์‹คํŒจํ–ˆ๋Š”์ง€, ๊ฑฐ๋ถ€ํ–ˆ๋Š”์ง€, ์„ฑ๊ณตํ–ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค. Spring ๊ธฐ

devel-repository.tistory.com

 

+ Recent posts