๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

[Spring] Request ์–ด๋…ธํ…Œ์ด์…˜ (parameter mapping)

by young-ji 2022. 12. 1.

Request๊ฐ€ ๋“ค์–ด์˜ค๋Š” ํƒ€์ž…์— ๋”ฐ๋ผ ๋ฐ›๋Š” ๋ฐฉ์‹์ด ๋‚˜๋‰œ๋‹ค.

  1. URI
  2. Query String
  3. body
  4. form

@PathVariable

@GetMapping( "/api/v1/customers/{customerId}")
@ResponseBody
public ResponseEntity<Customer> findCustomer(@PathVariable("customerId") UUID customerId){
    var customer = customerService.getCustomer(customerId);
    return customer.map(v -> ResponseEntity.ok(v)).orElse(ResponseEntity.notFound().build());
}

URI ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด Request๊ฐ€ ๋“ค์–ด์˜ค๋Š” ํƒ€์ž…

http://localhost/api/v1/customers/{customerId}

: ์ผ๋ฐ˜์ ์„ GET ๋ฐฉ์‹์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ „๋‹ฌ๋กœ ํ”ํžˆ ๋ณผ์ˆ˜์žˆ๋Š” URI๋ฅผ ์ด์šฉํ•˜์—ฌ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹

: GetMapping ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋‹น์—ฐํžˆ ๋‹ค๋ฅธ Method๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

 

ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์ „๋‹ฌ๋˜์ง€์•Š์•˜์„ ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•˜์—ฌ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€์•Š๊ฒŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฒ˜๋ฆฌ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

(required = false)์„ ์„ค์ •ํ•˜๋ฉด ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ null๋กœ ์„ค์ •ํ•œ๋‹ค.

@GetMapping(value = { "/api/v1/customers", "/api/v1/customers/{customerId}" })
@ResponseBody
public String getEmployeesByIdWithRequired(@PathVariable(required = false) UUID customerId) {
    if(custmoerId == null) 
        //....
    else {
      	var cutomer = customerService.getCustomer(customerId);
        //....
    }
}

https://www.baeldung.com/spring-pathvariable

 

 

@RequestParam

@GetMapping("api/v1/products")
@ResponseBody
public List<Product> productList(@RequestParam Optional<String> category){
    return category
            .map(productService::getProductByCategory)
            .orElse(productService.getAllProducts()); 
						// optional map ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ์ฒด๋ฅผ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋น„์–ด ์žˆ์„ ๊ฒฝ์šฐ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค.
}

โ€ข ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญ์‹œ ์ „๋‹ฌํ•˜๋Š” ๊ฐ’์„ Controller ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ 1:1 ๋งตํ•‘ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜

 

Query String์œผ๋กœ ๊ฐ’์ด ์ „๋‹ฌ๋  ๊ฒฝ์šฐ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ’์œผ๋กœ ๋„˜๊ฒจ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

http://localhost/api/v1/products?category=

: GET ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์œ„์™€๊ฐ™์€ URL๋กœ ๊ฐ’์„ ์ „๋‹ฌ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

 

HTML ํƒœ๊ทธ์˜ Form ๋ฐ์ดํ„ฐ๋ฅผ POST ๋ฉ”์†Œ๋“œ๋กœ ์ „์†กํ• ๋•Œ๋„ ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

: @RequestParam ์€ url ์ƒ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๋Š”๋‹ค. <form> ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ œ์ถœ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๋“ค์ด ์œ„์™€๊ฐ™์€ url์„ ํ†ตํ•ด์„œ ์ „๋‹ฌ๋œ๋‹ค.

https://amagrammer91.tistory.com/106

 

 

@RequestBody

@PostMapping("/api/v1/orders")
@ResponseBody
public Order createOrder(@RequestBody CreateOrderRequest createOrderRequest){
    return orderService.createOrder(
           new Email(createOrderRequest.email()),
            createOrderRequest.address(),
            createOrderRequest.postcode(),
            createOrderRequest.orderItems()
    );
}

Json ํ˜•ํƒœ์˜ HTTP Body๋ฅผ ํ†ตํ•œ request๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

: MessageConverter๋ฅผ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ ๊ณผ์ •์„ ๊ฑฐ์ณ java ๊ฐ์ฒด๋ฅผ ์ž๋™๊ฐ์ฒด ์ƒ์„ฑํ•ด์ค€๋‹ค.

: @RequestBody๋ฅผ ์‚ฌ์šฉํ•  ๊ฐ์ฒด๋Š” ํ•„๋“œ๋ฅผ ๋ฐ”์ธ๋”ฉํ•  ์ƒ์„ฑ์ž๋‚˜ setter ๋ฉ”์„œ๋“œ๊ฐ€ ํ•„์š”์—†๋‹ค.

  • ๋‹ค๋งŒ ์ง๋ ฌํ™”๋ฅผ ์œ„ํ•ด ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋Š” ํ•„์ˆ˜๋‹ค.
  • ๋˜ํ•œ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์„ ์œ„ํ•œ ํ•„๋“œ๋ช…์„ ์•Œ์•„๋‚ด๊ธฐ ์œ„ํ•ด getter๋‚˜ setter ์ค‘ 1๊ฐ€์ง€๋Š” ์ •์˜๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

 


 

Query String ๋ฐ Form ํ˜•์‹์œผ๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ java ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜์‹œ์ผœ์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด @ModelAttribute ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•œ๋‹ค.

https://tecoble.techcourse.co.kr/post/2021-05-11-requestbody-modelattribute/

 

@RequestBody vs @ModelAttribute

1. @RequestBody์™€ @ModelAttribute Controller.java @RequestBody์™€ @ModelAttribute๋Š” ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ๋ณด๋‚ธ ๋ฐ์ดํ„ฐ๋ฅผ Javaโ€ฆ

tecoble.techcourse.co.kr

 

 

 

์ž˜๋ชป๋œ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€์„ ํ†ตํ•ด ์•Œ๋ ค์ฃผ์„ธ์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. 

๋Œ“๊ธ€