작성
·
96
0
학습하는 분들께 도움이 되고, 더 좋은 답변을 드릴 수 있도록 질문전에 다음을 꼭 확인해주세요.
1. 강의 내용과 관련된 질문을 남겨주세요.
2. 인프런의 질문 게시판과 자주 하는 질문(링크)을 먼저 확인해주세요.
(자주 하는 질문 링크: https://bit.ly/3fX6ygx)
3. 질문 잘하기 메뉴얼(링크)을 먼저 읽어주세요.
(질문 잘하기 메뉴얼 링크: https://bit.ly/2UfeqCG)
질문 시에는 위 내용은 삭제하고 다음 내용을 남겨주세요.
=========================================
[질문 템플릿]
1. 강의 내용과 관련된 질문인가요? (예/아니오)
2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예/아니오)
3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예/아니오)
[질문 내용]
여기에 질문 내용을 남겨주세요.
위와같이 강의 따라서 @getMapping("/new-form") 이렇게만 해줘도 HTTP GET 메서드만 받을 수 있도록 제약할 수 있고, URL 매핑까지 된다는 걸 배웠는데요,
@GetMapping("/new-form")
이렇게 @GetMapping 다음에 괄호를 열고 매핑할 url을 넣어주고 괄호를 닫으면 된다는 걸 저는 @GetMapping 소스코드에서 방법을 알려주는지 궁금했습니다.
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* Annotation for mapping HTTP {@code GET} requests onto specific handler
* methods.
*
* <p>Specifically, {@code @GetMapping} is a <em>composed annotation</em> that
* acts as a shortcut for {@code @RequestMapping(method = RequestMethod.GET)}.
*
* <p><strong>NOTE:</strong> This annotation cannot be used in conjunction with
* other {@code @RequestMapping} annotations that are declared on the same method.
* If multiple {@code @RequestMapping} annotations are detected on the same method,
* a warning will be logged, and only the first mapping will be used. This applies
* to {@code @RequestMapping} as well as composed {@code @RequestMapping} annotations
* such as {@code @PutMapping}, {@code @PostMapping}, etc.
*
* @author Sam Brannen
* @since 4.3
* @see PostMapping
* @see PutMapping
* @see DeleteMapping
* @see PatchMapping
* @see RequestMapping
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.GET)
public @interface GetMapping {
/**
* Alias for {@link RequestMapping#name}.
*/
@AliasFor(annotation = RequestMapping.class)
String name() default "";
/**
* Alias for {@link RequestMapping#value}.
*/
@AliasFor(annotation = RequestMapping.class)
String[] value() default {};
/**
* Alias for {@link RequestMapping#path}.
*/
@AliasFor(annotation = RequestMapping.class)
String[] path() default {};
/**
* Alias for {@link RequestMapping#params}.
*/
@AliasFor(annotation = RequestMapping.class)
String[] params() default {};
/**
* Alias for {@link RequestMapping#headers}.
*/
@AliasFor(annotation = RequestMapping.class)
String[] headers() default {};
/**
* Alias for {@link RequestMapping#consumes}.
* @since 4.3.5
*/
@AliasFor(annotation = RequestMapping.class)
String[] consumes() default {};
/**
* Alias for {@link RequestMapping#produces}.
*/
@AliasFor(annotation = RequestMapping.class)
String[] produces() default {};
}
그래서 위와같이 @GetMapping 소스코드를 확인했는데, 뚜렸하게 방법을 알려준다는 건 제가 찾지 못했는데 혹시 제가 못찾는 것일까요? 아니면 제일 첫 줄에 있는 "Annotation for mapping HTTP GET requests onto specific handler methods." 이 문장으로 @GetMapping에 값을 그대로 적어주면 된다(괄호를 열고 url쓰고 닫으면 된다)고 알아들어야 하려나요?
답변 2
1
0
안녕하세요, 인프런 AI 인턴입니다. ajdsasld님.
스프링 프레임워크에서 @GetMapping
어노테이션은 HTTP GET 요청을 특정 처리 메서드에 매핑하는 데 사용되는 어노테이션입니다. 이 어노테이션은 사실상 @RequestMapping(method = RequestMethod.GET)
의 줄임 표현입니다.
소스 코드를 보면, @GetMapping
어노테이션은 @RequestMapping
의 일부 기능을 제공하는 구조로 구성되어 있습니다. 여기에서 value
, path
같은 속성은 @RequestMapping
의 동일한 속성을 대체하는 역할을 합니다. 이는 코드에서 보면 @AliasFor
를 통해 알 수 있습니다.
즉, @GetMapping("/new-form")
는 @RequestMapping(value = "/new-form", method = RequestMethod.GET)
와 동일한 것으로, 괄호 내의 문자열은 매핑할 URL을 나타냅니다. 따라서, @GetMapping("/new-form")
로 선언하면 해당 경로로 요청이 들어왔을 때 해당 메서드가 호출됩니다.
질문하신 부분에서 “Annotation for mapping HTTP GET requests onto specific handler methods.“라는 설명은 바로 이 기능을 설명하는 것입니다. 이 한 줄은 @GetMapping
이란 어노테이션이 어떻게 사용되는지를 간단히 알려주는 주석입니다. 코드 안에서 괄호 안에 URL을 명시함으로써, 이 URL로의 GET 요청을 처리할 수 있다는 사실을 이해하게 됩니다.
이와 관련된 더 많은 정보를 원하면 아래 URL에서 유사한 질문 및 답변들을 참조하실 수 있습니다:
- @GetMapping과 MemberController클래스 - @GetMapping
어노테이션의 사용과 메서드 매핑에 관한 설명.
저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다.
현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏
추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.
@RequestMapping에 나와있군요! 자세히 찾아주셔서 감사합니다ㅠㅠ 혹시 이런 문제들은 원래 알고 계셨던 걸까요? 아니면 구글링을 하신건지 궁금합니다! 저는 항상 제가 가진 궁금증과 문제들을 혼자 해결하기가 어려운데 어떻게 알고 계신건지, 혹은 어떤 방식으로 이러한 질문들을 해결하시는지도 궁금합니다~