@ResponseBody
@RequestMapping("/request-param-required")
public String requestParamRequired(@RequestParam(required = true) String username,
@RequestParam(required = false) int age){
log.info("username={}",username);
log.info("age = {}",age);
return "ok";
}
1] http://localhost:8080/request-param-required?username=""
-> required = true는 <key와 value>가 무조건 있어야 한다. username="" 은 빈 문자열이라는 데이터가 들어 온 것이기에 서버에서 정상 처리가 된다.
2] http://localhost:8080/request-param-required?
-> username 자체가 없어야만 서버에서 404(Bad Request) Error를 내려준다.
(required = false인 경우 해당 매개변수가 없으면 null을 반환,이때 age의 자료형은 기본형이 아닌 Integer형으로 해줘야 한다)
3] http://localhost:8080/request-param-required?username=
-> 이 경우에는 매개변수 입력을 안 했다고 스프링이 판단!
'Springあるある' 카테고리의 다른 글
static class에 @Component 사용 시 주의점! (0) | 2025.01.06 |
---|---|
Request Body 전체 조회!!(Feat. HttpEntity) (0) | 2025.01.03 |
@ModelAttribute 실행 프로세스 (0) | 2025.01.03 |
@ResponseBody(Feat. produces) (0) | 2025.01.03 |
스프링 로그(feat. SLF4J ,Logback) (1) | 2025.01.03 |