본문 바로가기
프로그래밍언어/Java

[Java] API형식으로 요청 매핑 받기

by Yikanghee 2022. 1. 30.
  • 스프링에서 지원하는 API를 사용하여 요청 매핑을 받을 수 있다
  • RequestMapping을 Class에 어노테이션 해서 디폴트 URL값을 인식해준다
  • GET방식
@GetMapping
    public String user() {
        return "get users";
    }
  • POST방식
@PostMapping
    public String addUser() {
        return "post user";
    }
  • GET방식 매개변수 포함하여 받기
@GetMapping("/{userId}")
    public String findUser(@PathVariable String userId) {
        return "get userId" + userId;
    }
  • UPDATE
@PatchMapping("/{userId}")
    public String updateUser(@PathVariable String userId) {
        return "update userId" + userId;
    }
  • DELETE
@DeleteMapping("/{userId}")
    public String deleteUser(@PathVariable String userId) {
        return "delete userId" + userId;
    }
  • POSTMAN으로 값을 확인

댓글