[Spring] ResponseEntity의 유용한 메소드들 [200 OK, 400 Bad Request, 404 Not Found, 201 Created 등]
·
▣ Framework/Spring🍃
- ResponseEntity의 유용한 메소드들1. ResponseEntity.ok(): 200 OK 응답 반환return ResponseEntity.ok("성공");2. ResponseEntity.badRequest(): 400 Bad Request 응답 반환return ResponseEntity.badRequest().body("잘못된 요청입니다.");3. ResponseEntity.notFound(): 404 Not Found 응답 반환return ResponseEntity.notFound().build();4. ResponseEntity.created(): 201 Created 응답 반환 (새로운 리소스 생성 시)URI location = URI.create("/resource/1");return..
[Spring] ResponseEntity란? [HTTP 응답을 생성할 때 매우 유용한 클래스]
·
▣ Framework/Spring🍃
- ResponseEntity란?ResponseEntity는 Spring에서 HTTP 응답을 생성할 때 매우 유용한 클래스이다. 상태 코드, 헤더, 본문을 자유롭게 조작할 수 있기 때문에 다양한 HTTP 응답을 세밀하게 제어할 수 있다.- 기본 사용법 // 기본적인 ResponseEntity 사용 예시ResponseEntity response = new ResponseEntity(body, HttpStatus.OK);- 주요 사용 예시들 1) 기본적인 응답 ResponseEntity.ok() : 상태 코드 200 OK와 함께 응답 본문 반환 @ResponseBody@PostMapping("/example")public ResponseEntity example() { // 정상적으로 처리된 응답 ..