ν‹°μŠ€ν† λ¦¬ λ·°

😎

@PathVariable

十月 2024. 7. 19. 21:49

🍑 PathVariable 은 무엇인가?

  • 경둜 λ³€μˆ˜ ν‘œμ‹œλ₯Ό μœ„ν•΄ λ©”μ†Œλ“œ λ§€κ°œλ³€μˆ˜μ— μ‚¬μš©λ¨
  • 경둜 λ³€μˆ˜ : μ€‘κ΄„ν˜Έλ‘œ λ‘˜λŸ¬μ‹ΈμΈ κ°’ → {id}
  • url κ²½λ‘œμ—μ„œ λ³€μˆ˜ κ°’ μΆ”μΆœ ν›„ λ§€κ°œλ³€μˆ˜μ— ν• λ‹Ή
  • 경둜 λ³€μˆ˜λŠ” λ°˜λ“œμ‹œ 값을 κ°€μ Έμ•Ό ν•˜λ©°, 값이 μ—†λ‹€λ©΄ 404 였λ₯˜ λ°œμƒ
  • 상세 쑰회, μˆ˜μ •, μ‚­μ œμ™€ 같은 μž‘μ—…μ—μ„œ λ¦¬μ†ŒμŠ€ μ‹λ³„μžλ‘œ μ‚¬μš©

 

🍏 μ‚¬μš©λ²•

// import μƒλž΅

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @GetMapping("/{id}")
    public @ResponseBody ApiResponse<ProductSelectResponseDto> selectProduct(@PathVariable Long id) throws Exception{
        return ApiResponse.OK(productService.selectProduct(id));
    }
	
    // μΆ”μΆœν•  λ³€μˆ˜ 값이 2κ°€μ§€ 이상인 경우
    
    @GetMapping("/{id}/{name}")
    public String user(@PathVariable Long id, @PathVariable String name) {
        System.out.println("μ œν’ˆ λ²ˆν˜ΈλŠ” " + id);
        System.out.println("이름은 " + name);
        return "index";
    }
    
}

 

예λ₯Ό λ“€μ–΄, μ•„λž˜μ™€ 같은 URLμ—μ„œ μƒ‰μΉ ν•œ 뢀뢄을 @PathVariable둜 μ²˜λ¦¬ν•  수 μžˆλ‹€.
β‘  blog.naver.com/1234
β‘‘ seewol.tistory.com/5/μˆ˜μ €μ„ΈνŠΈ

 

λŒ“κΈ€
곡지사항
μ΅œκ·Όμ— 올라온 κΈ€
μ΅œκ·Όμ— 달린 λŒ“κΈ€
Total
Today
Yesterday
링크
TAG more
Β«   2025/06   Β»
일 μ›” ν™” 수 λͺ© 금 ν† 
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
κΈ€ 보관함