안녕하세요 RequestParam 생략 질문있습니다.
508
작성한 질문수 1
안녕하세요 MVC 1편을 수강 중입니다. 강의에서는 파라미터와 이름이 같으면 생략이 가능하다고 하는데,
저는 그 지점이 잘 안되는 것 같아요. 코드랑 오류 올리겠습니다 ㅠ 어디가 문제인지 찾아주시면 감사하겠습니다.
package hello.springmvc.basic.request;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.io.IOException;
@Controller
@Slf4j
public class RequestParamController {
@RequestMapping("/request-param-v1")
public void requestParamV1(HttpServletRequest request, HttpServletResponse response) throws IOException {
String username = request.getParameter("username");
int age = Integer.parseInt(request.getParameter("age"));
log.info("username={}, age={}", username,age);
response.getWriter().write("ok");
}
@ResponseBody
@RequestMapping("/request-param-v2")
public String requestParamV2(@RequestParam("username") String memberName,
@RequestParam("age") int memberAge
)
{
log.info("username={}, age={}", memberName,memberAge);
return "ok";
}
@ResponseBody
@RequestMapping("/request-param-v3")
public String requestParamV3(@RequestParam String username,
@RequestParam int age
)
{
log.info("username={}, age={}", username,age);
return "ok";
}
@ResponseBody
@RequestMapping("/request-param-v4")
public String requestParamV4(String username, int age)
{
log.info("username={}, age={}", username,age);
return "ok";
}
@ResponseBody
@RequestMapping("/request-param-required")
public String requestParamRequired(@RequestParam(required = false) String username, @RequestParam(required = false) int age)
{
log.info("username={}, age={}", username,age);
return "ok";
}
@ResponseBody
@RequestMapping("/request-param-default")
public String requestParamDefault(@RequestParam(required = false, defaultValue = "guest") String username, @RequestParam(required = false, defaultValue = "-1") int age)
{
log.info("username={}, age={}", username,age);
return "ok";
}
}java.lang.IllegalArgumentException: Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.
답변 2
0
안녕하세요, 인프런 AI 인턴입니다.
cheche903님께서 겪고 계신 문제는 @RequestParam 파라미터 이름 생략 시 발생하는 오류와 관련된 것으로 보입니다. 스프링 프레임워크에서 메서드 파라미터의 이름을 저장하기 위해서는 컴파일 시 -parameters 옵션을 활성화해야 합니다. 하지만, 이 옵션이 활성화되지 않은 상태에서 @RequestParam 어노테이션에 이름을 명시하지 않고 사용하게 되면, 스프링은 메서드 파라미터의 이름을 알 길이 없어 위와 같은 오류가 발생하게 됩니다.
이와 관련하여 유사한 문제를 해결한 예를 찾았습니다. @RequestParam 어노테이션의 파라미터 이름 생략과 관련된 질문인데요, 아래는 비슷한 상황에서 도움을 받을 수 있는 링크입니다:
- @RequestParam(“username”) 생략 관련 질문: 이 질문은
@RequestParam에 관한 질문으로, 질문자는@RequestParam어노테이션에서 파라미터 이름을 생략하려고 할 때 발생하는 오류에 대해 물었습니다. 답변에서는 IntelliJ 또는 다른 IDE에서 컴파일러 옵션을 설정하는 방법을 소개하고 있습니다. 여기에 언급된 방법으로 문제를 해결할 수도 있습니다.
해당 질문에 대한 답변에서는 IntelliJ IDEA에 대한 설정 방법을 소개하고 있으며 이는 -parameters 컴파일 옵션을 활성화하는 데 도움이 될 수 있습니다. 해당 설정 후에도 문제가 해결되지 않는다면, -parameters 옵션이 정확히 활성화되었는지 확인하시기 바랍니다.
혹시 사용하고 계신 개발 환경이 IntelliJ IDEA가 아니거나, 위의 방법으로 해결되지 않는 경우, 사용하고 있는 IDE나 빌드 도구(예: Maven, Gradle)에 따라 -parameters 컴파일 옵션을 활성화하는 방법을 찾아보시길 추천드립니다.
저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다. 현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏 추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.
servlet과 container에 대한 질문입니다
0
24
1
api를 어느 컨트롤러에 작성해야하는지는 어떤 기준으로 해야하나요?
0
62
1
jsp 의존성 수정 요청
0
77
2
요즘 웹 서버가 주로 사용되는 이유는 SPA 구조 때문일까요 ?
0
142
1
save() 메서드 문의
0
66
1
절대 경로로 templates/basic 하위 파일 열면 css 적용 안되는 현상
0
99
1
request-body-json
0
83
2
MVC 패턴의 적용 단위
0
94
1
RequestMapping을 이용한 핸들러, 어댑터
0
118
2
save 후 결과화면
0
88
2
jsp를 이용한 view
0
97
1
application.properties에 debug 추가해도 결과가 똑같습니다.
0
177
1
수업 코드 제공 관련 문의
0
97
2
RequestMappingHandlerAdapter의 Controller 호출 과정
0
99
3
파일 오픈 시
0
68
1
스프링 배치 관련
0
77
1
@RequestParam의 defaultValue가 blank 값도 처리하는 지 여부
0
112
1
postman으로 /request-body-json-v1 호출시 500 error
0
94
1
프론트엔드와 백엔드의 mvc, rest api에 대한 질문
0
78
1
모델의 역할과 계층 분리에 대한 이해 차이 + 추가질문
0
111
1
console log 출력 관련 질문입니다.
0
74
1
애플리케이션이 실행 되지 않습니다 ㅠㅠㅠ
0
139
1
html 변경하는 부분 적용 문제
0
103
1
한글 깨짐
0
76
2





