- Spring Mapping 요청을 받는 예시를 알아보자
- 기본
@RestController
@RequestMapping(value = "/hello-basic", method = RequestMethod.GET)
public String helloBasic() {
log.info("helloBasic");
return "ok";
}
@RestController
@GetMapping(value = "/hello.basic")
public String helloBasic() {
log.info("helloBasic");
return "ok";
}
- requestMapping으로 주소를 매핑받고
- 방식은 Get방식으로 설정해준다
- 리턴 값을 “ok”로 받기 위해서 RestController를 사용한다
- 두 번째 방법은 method = RequestMethod.GET를 GetMapping을 사용하여 생략해준다
@GetMapping("/mapping/{userId}")
public String mappingPath(@PathVariable("userId") String data) {
log.info("mappingPath userId={}", data);
return "ok";
}
- url주소와 파라미터를 받을 경우 @PathVariable을 사용하여 받을 수 있다
- log를 사용하여 값을 출력할 때에는 = {} 형식을 사용하여 연산 사용을 최소화한다
@GetMapping("/mapping/{userId}/orders/{orderId}")
public String multimappingPath(@PathVariable("userId") String userId, @PathVariable Long orderId) {
log.info("mappingPath userId={}, orderId={}", userId, orderId);
return "ok";
}
- 위에 것과 마찬가지로 @PathVariable을 사용하여 받을 수 있다
댓글