[ Java / Spring ] @RequestParam
2024. 2. 7. 19:22ㆍ· LANGUAGE/└ Java
환경 : Spring Tool Suite 4
-- 파라미터 고급 설정 --
@RequestParam 는 스프링에서 지원하는 HTTP 요청 파라미터 값을 편리하게 사용하게 해주도록 도와주는 설정이 가능하다.
[ 파라미터의 값이 없는 경우 처리 ]
@RestController
public class ParameterController {
@RequestMapping("/coffee")
public String coffee(@RequestParam(required = false, defaultValue = "americano") String kind,
@RequestParam(required = false, defaultValue = "0") int shot) {
return kind+"주문, 샷" + shot+"개 추가";
}
}
(+) required = false 만 적용하게 되면,
해당 파라미터의 값이 없을 때 null 값이 들어오게 된다.
(+) defaultValue = "" 를 적용하게 되면,
해당 파라미터의 값이 없을 때 적용할 기본 값을 설정할 수 있다.
위 식을 아래와 같은 파라미터 값으로 주었을 때 출력결과이다.
http://localhost:8080/dutch?price=5000&people=3 //두개의 변수 값을 모두 주었을 때
http://localhost:8080/dutch?price=5000 // 가격 변수만 주었을 때
http://localhost:8080/dutch?people=3 // 사람 변수만 주었을 때
http://localhost:8080/dutch // 모든 변수의 값을 주지 않았을 때
개인 공부 기록용입니다:)
728x90
'· LANGUAGE > └ Java' 카테고리의 다른 글
[ Java ] TreeSet 기본 구조 이해 및 실습 (0) | 2024.02.16 |
---|---|
[ Spring / Java ] DecimalFromat() 메서드 (0) | 2024.02.13 |
[ Java ] DTO / DAO 이해 (0) | 2024.01.29 |
[ Java /JDBC ] RowMapper / ResultSetExtractor의 차이점 (0) | 2024.01.28 |
[ Java ] 삼항연산자(Ternary Operator) (0) | 2024.01.27 |