
Request๊ฐ ๋ค์ด์ค๋ ํ์ ์ ๋ฐ๋ผ ๋ฐ๋ ๋ฐฉ์์ด ๋๋๋ค.
- URI
- Query String
- body
- 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
์๋ชป๋ ์ ๋ณด๊ฐ ์๋ค๋ฉด ๋๊ธ์ ํตํด ์๋ ค์ฃผ์ธ์. ๊ฐ์ฌํฉ๋๋ค.
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring] Filter์ Interceptor ์ฐจ์ด (2) | 2023.04.22 |
---|---|
[JPA] ์ปค์ ๊ธฐ๋ฐ pagenation ๊ตฌํํ๊ธฐ (8) | 2023.03.01 |
[JPA] ์คํ์ ๊ธฐ๋ฐ Pagenation ๊ตฌํํ๊ธฐ (0) | 2023.03.01 |
[Spring] Spring MVC (0) | 2023.02.18 |
[Java] List ์ ํจ์ฑ ๊ฒ์ฌ (1) | 2022.12.01 |
๋๊ธ