@RequestParam
HTTP 요청에서 전달된 "파라미터 값"을 컨트롤러 메서드의 매개변수로 바인딩해주는 어노테이션이다.
쉽게 말하면, 클라이언트가 요청할 때 URL이나 폼에 같이 보낸 값을 변수로 받아주는 역할이다.
🔍 예제 1: GET 요청에서 사용
📌 요청 URL
GET /greet?name=홍길동
📌 컨트롤러
@GetMapping("/greet")
public String greet(@RequestParam String name) {
return "안녕하세요, " + name + "님!";
}
- ?name=홍길동 → @RequestParam String name으로 전달
- 결과 : "안녕하세요, 홍길동님!"
🔍 예제 2: POST 폼 데이터에서 사용
📌 HTML 폼
<form method="post" action="/login">
<input name="email">
<input name="password" type="password">
<button type="submit">로그인</button>
</form>
📌 컨트롤러
@PostMapping("/login")
public String login(@RequestParam String email,
@RequestParam String password) {
// email, password 값이 자동으로 바인딩됨
return "로그인 시도: " + email;
}
🔧 주요 속성
속성 | 설명 |
value 또는 name | 요청 파라미터 이름 지정(@RequestParam("id")) |
required | 필수 여부 (기본값 : true) |
defaultValue | 값이 없을 때 기본값 지정 |
'자바 > 스프링' 카테고리의 다른 글
[스프링] 프로필(Profile)이란? (1) | 2025.07.08 |
---|---|
[스프링] 요청 매핑 어노테이션 (0) | 2025.07.03 |
[스프링] @ResponseBody, ResponseEntity (1) | 2025.07.03 |
[스프링] application.properties 파일 (2) | 2025.07.02 |
[스프링 프레임워크] 기본 개념 정리 1 (22) | 2023.10.18 |