KoreaIt Academy/Spring Boot
[Spring Boot] REST 방식 - ResponseEntity을 사용한 댓글 수정(PUT, PATCH) , 삭제(DELETE)
728x90
반응형
댓글 수정
HTTP Method 중에 PUT와 PATCH는 리소스의 업데이트를 의미합니다.
리소스를 업데이트 한다는 점에서는 같은 역할을 하는 메소드처럼 보이지만
두개의 요청에는 약간의 차이가 있습니다.
PUT : 자원의 전체 수정, 자원 내 모든 필드를 전달해야 함, 일부만 전달할 경우 오류
PATCH : 자원의 일부 수정, 수정할 필드만 전송(자동 주입이 아닌 부분만 수정하는 쿼리문에서 사용)
PATCH가 PUT을 담고 있기 때문에 전체를 전달 받아서 전체를 수정하는 상황,
전체 중 부분만 수정하는 상황 모두 PATCH를 사용하는 것이 좋습니다.
@RequestMapping(method={RequestMethod.PUT, RequestMethod.PATCH}, value="{rno}" ,
consumes = "application/json", produces = "text/plain; charset=utf-8")
public ResponseEntity<String> modify(@RequestBody ReplyVO replyVO, @PathVariable("rno") Long rno) throws UnsupportedEncodingException{
replyVO.setRno(rno);
return replyService.modify(replyVO) == 1 ? new ResponseEntity<>(new String("댓글 수정성공".getBytes(),"utf-8"),HttpStatus.OK) :
new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
//mybatis에서의 update문
<update id="update">UPDATE TBL_BOARD_REPLY SET REPLY = #{reply}, UPDATEDATE = SYSDATE WHERE RNO = #{rno} </update>
@PathVariable로 설정한 URI의 rno가 아니라 replyVO.getRno를 할 수있게끔
replyVO.setRno(rno)를 써 주어야 합니다.
댓글 삭제
※ DELETE 메소드를 사용합니다.
@DeleteMapping(value = "{rno}" , produces = "text/plain; charset=utf-8")
public ResponseEntity<String> remove(@PathVariable("rno") Long rno) throws UnsupportedEncodingException{
return replyService.remove(rno) == 1 ? new ResponseEntity<>(new String("댓글 삭제 성공".getBytes(),"utf-8"),HttpStatus.OK) :
new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
728x90
반응형
'KoreaIt Academy > Spring Boot' 카테고리의 다른 글
[Spring Boot] JS모듈화(Ajax, callback)를 통한 REST Controller와의 통신 (0) | 2021.10.15 |
---|---|
[Spring Boot] @Controller와 @RestController의 차이, REST 방식 사용법(Ajax) - 댓글 등록, 조회 (0) | 2021.10.14 |
[Spring Boot] 단일 검색, 다중 검색 처리, MyBatis의 동적 태그 (0) | 2021.10.13 |
[Spring Boot] UriComponentsBuilder - 파라미터 연결(페이지 유지) (0) | 2021.10.13 |
[Spring Boot] 페이징 처리 - Criteria, PageDTO (2) | 2021.10.13 |
댓글