[Annotation] @RequestMapping, @PostMapping, @GetMapping

이주현·2022년 1월 5일
0

Annotation

목록 보기
2/5
post-thumbnail

@RequestMapping

  • client의 요청이 왔을 때, 해당 url에 해당하는 Controller, Method 를 호출합니다.
  • Controller 와 Method 에 Annotation 을 모두 적용하면, 적용된 값을 조합하여 매핑될 경로를 설정합니다.

// /api/Password
// /api/Password/v1
// 해당하는 요청이 있을 시, 
@RequestMapping("/api")
public class Controller {
  
  // 다중 호출도 가능합니다.
  @RequestMapping({"/Password", "/Password/v1"})
  public ApiResponse password(){
    return ApiResponse.OK;
  }
}

  • 값 설정
@RequestMapping(value="/api") == @RequestMapping("/api")

  • 요청방식 설정
// GET, POST 둘다 가능합니다.
// 동시에 적용 가능
@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})

  • 경로변수 사용
    @PathVariable("myPath")
// 경로에 변수를 넣어 
@RequestMapping({"/{myid}"})
public ApiResponse id(@PathVariable("myid") String id)
  log.debug("myid = {}", id)
  return ApiResponse.OK;
}

@PostMapping

  • Post 로 Mapping 을 하는 방법입니다.
  • @RequestMapping 을 간단하게 줄여쓸 수 있습니다.
//@RequestMapping("/api", method = {RequestMethod.POST})
@PostMapping("/api")
public ApiResponse password(){
    return ApiResponse.OK;
}

@GetMapping

  • Get 로 Mapping 을 하는 방법입니다.
  • @RequestMapping 을 간단하게 줄여쓸 수 있습니다.
//@RequestMapping("/api", method = {RequestMethod.GET})
@GetMapping("/api")
public ApiResponse password(){
    return ApiResponse.OK;
}
profile
아직

0개의 댓글