• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

안녕하세요 RequestParam 생략 질문있습니다.

24.04.01 11:37 작성 조회수 103

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

codesweaver님의 프로필

codesweaver

2024.04.01

안녕하세요. cheche903님, 공식 서포터즈 코즈위버입니다.

아래의 링크 내용도 참조해주세요.

https://www.inflearn.com/questions/1087879/pathvariable-name-%EC%83%9D%EB%9E%B5-%EC%A7%88%EB%AC%B8-%EB%93%9C%EB%A6%BD%EB%8B%88%EB%8B%A4

감사합니다.

 

cheche903님의 프로필

cheche903

질문자

2024.04.01

감사합니다

 

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.04.01

안녕하세요, 인프런 AI 인턴입니다.

cheche903님께서 겪고 계신 문제는 @RequestParam 파라미터 이름 생략 시 발생하는 오류와 관련된 것으로 보입니다. 스프링 프레임워크에서 메서드 파라미터의 이름을 저장하기 위해서는 컴파일 시 -parameters 옵션을 활성화해야 합니다. 하지만, 이 옵션이 활성화되지 않은 상태에서 @RequestParam 어노테이션에 이름을 명시하지 않고 사용하게 되면, 스프링은 메서드 파라미터의 이름을 알 길이 없어 위와 같은 오류가 발생하게 됩니다.

이와 관련하여 유사한 문제를 해결한 예를 찾았습니다. @RequestParam 어노테이션의 파라미터 이름 생략과 관련된 질문인데요, 아래는 비슷한 상황에서 도움을 받을 수 있는 링크입니다:

  • @RequestParam(“username”) 생략 관련 질문: 이 질문은 @RequestParam에 관한 질문으로, 질문자는 @RequestParam 어노테이션에서 파라미터 이름을 생략하려고 할 때 발생하는 오류에 대해 물었습니다. 답변에서는 IntelliJ 또는 다른 IDE에서 컴파일러 옵션을 설정하는 방법을 소개하고 있습니다. 여기에 언급된 방법으로 문제를 해결할 수도 있습니다.

해당 질문에 대한 답변에서는 IntelliJ IDEA에 대한 설정 방법을 소개하고 있으며 이는 -parameters 컴파일 옵션을 활성화하는 데 도움이 될 수 있습니다. 해당 설정 후에도 문제가 해결되지 않는다면, -parameters 옵션이 정확히 활성화되었는지 확인하시기 바랍니다.

혹시 사용하고 계신 개발 환경이 IntelliJ IDEA가 아니거나, 위의 방법으로 해결되지 않는 경우, 사용하고 있는 IDE나 빌드 도구(예: Maven, Gradle)에 따라 -parameters 컴파일 옵션을 활성화하는 방법을 찾아보시길 추천드립니다.

저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다. 현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏 추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.