스프링부트 - url연결하기

씩씩한 조약돌·2023년 11월 18일
0

코드 기록🤓

목록 보기
21/31
  1. @GetMapping("/board/{id}")
    public String read(@PathVariable Long id) {
    return "";
    }

@PathVariable : URL요청으로 들어온 전달값을 컨트롤러의 매개변수로 가져오는 어노테이션

  1. @PostMapping("/board")
    public Board create(@RequestBody Board dto) {
    // ...
    }

@RequestBody : post요청시 본문(Body)에 보낸 json데이터를 메서드의 매개변수로 받아올 수 있게 함

  1. @PatchMapping("/board/{id}")
    public ResponseEntity update(@PathVariable Long id, @RequestBody Board dto) {
    //...
    Board target = ...;

    if (target == null) {
    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);

    return ResponseEntity.status(HttpStatus.OK).body(target);
    }

  • url로 id를 받고, requestbody로 수정할 데이터를 받아옴
  • ResponseEntity<> : REST컨트롤러의 반환형. REST API의 요청을 받아 응답할 때 이 클래스에 HTTP 상태코드, 헤더, 본문을 실어 보낼 수 있음
  • HttpStatus : HTTP상태코드를 관리하는 클래스
  1. @DeleteMapping("/board/{id}")
    Public ResponseEntity delete(@PathVariable Long id) {
    //...
    return ResponseEntity.status(HttpStatus.OK).build();
    }

.build() : ResponseEntity객체의 메서드. HTTP응답의 body가 없는 ResponseEntity객체를 생성함.
.body(null)과 같은 의미

profile
씩씩하게 공부중 (22.11~)

0개의 댓글