· LANGUAGE/└ Java
[ Java / Spring ] @RequestParam
감자도리22
2024. 2. 7. 19:22
환경 : 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