[스프링] @RequestMapping

Nux·2022년 8월 8일
0

@RequestMapping

  • 요청이 왔을 때, 어떤 컨트롤러가 호출되어야 하는지 알려주는 일종의 표지판

특징

접근형식 지정

@RequestMapping(path=”/경로명”, method=”메서드 형식”)
  • get일 때, RequestMethod.GET. post일 때, RequestMethod.POST
  • 동일한 경로에 다른 method 매핑 가능
@RequestMapping(value="/info", method=RequestMethod.GET)
public String getInfo(){
	…
}

@RequestMapping(value="/info", method=RequestMethod.POST)
public String postInfo(){
	…
}

다중요청

@RequestMapping({“/test1”,”/test2”})
  • 콤마(,)를 구분자로 여러 경로를 하나의 메서드에 매핑 가능

경로변수

@RequestMapping(“/info/{userId}”)
  • 경로 값을 매개변수로 받아 올 수 있음
  • RequestMapping에서 설정한 변수와 메서드 매개변수명을 일치시켜야함
@RequestMapping(“/info/{userId}”)
public void memberInfo(@PathVariable String userId){
	…
}

@PathVariable[(value=”변수명”)]
매개변수의 타입 앞에 쓰여 매핑할 경로변수를 설정 할 수 있음

처리/요청 할 컨텐트타입 지정

@RequestMapping(value="/member", method=@RequestMethod.POST, produces="application/json")
  • 응답 Content-Type을 "application/json"로 변경
@RequestMapping(value="/member", method=@RequestMethod.POST, consumes="application/json")
  • 요청 Content-Type을 "application/json"로 변경

0개의 댓글