인프런 커뮤니티 질문&답변

maker_suhyeon님의 프로필 이미지

작성한 질문수

토비의 스프링 부트 - 이해와 원리

Hello 컨트롤러

섹션2 hello project

24.06.26 13:32 작성

·

96

·

수정됨

0

 @RestController

public class helloController

{

@GetMapping("/hello")

public String hello(String name)

{ return "hello"+name; }

}

안녕하세요, @RequestParam을 안쓰는 이유가 무엇인가요?

답변 1

0

토비님의 프로필 이미지
토비
지식공유자

2024. 06. 26. 14:13

스프링MVC에서 @RequestParam은 필수는 아닙니다.

기본적으로 단순한 타입이거나 다른 argument resolver에 의해서 바인딩 되는 경우가 아니라면 생략할 수 있습니다. 만약 가져오는 웹 파라미터 이름이 메소드 파라미터와 다르거나, 다른 옵션을 줘야하면 사용해야 합니다.

저는 특별한 이유가 있지 않으면 대체로 생략하는 편입니다. 그게 코드를 읽을 때도 간략해서 더 좋더라고요.

이에 대한 내용은 스프링 레퍼런스 문서에 잘 나와있습니다. 아래 링크의 페이지 가장 하단을 보시면 다음과 같은 설명이 있습니다.

Note that use of @RequestParam is optional (for example, to set its attributes). By default, any argument that is a simple value type (as determined by BeanUtils#isSimpleProperty) and is not resolved by any other argument resolver, is treated as if it were annotated with @RequestParam.

https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-methods/requestparam.html#page-title

maker_suhyeon님의 프로필 이미지
maker_suhyeon
질문자

2024. 06. 26. 14:40

감사합니다