ResponseEntity

Minseo Kang·2023년 2월 11일
0
post-thumbnail

01. ResponseEntity


개념

  • 응답에 대한 Customizing 필요할 때 ResponseEntity 사용

사용 코드

@PutMapping("/put")
public ResponseEntity<User> put(@RequestBody User user){
	return ResponseEntity.status(HttpStatus.CREATED).body(user);
}



02. ResponseEntity 클래스 정의


  • 타입 매개변수 T
  • HttpEntity를 상속받음
  • 오직 한 번만 할당할 수 있는 entity를 정의할 때 사용하는 final 변수로 status 선언되어 있음
public class ResponseEntity<T> extends HttpEntity<T> { 
    private final Object status;
}



03. HttpStatus 클래스 정의


  • enum 타입 리턴
public enum HttpStatus { }



04. enum


enum 개념

  • Enumeration의 약자
  • 요소, 멤버라 불리는 명명된 값의 집합을 이루는 자료형
  • 컬렉션을 정의하는 데 쓰이는 특수한 자바 유형(type)
  • 열거자 이름들은 일반적으로 해당 언어의 상수 역할을 하는 식별자
  • enum 키워드 사용
    • 상수를 쉼표로 구분
    • 대문자로 정의

HttpStatus 클래스에서 사용 예시

public enum HttpStatus {

	// 1xx Informational

	/**
	 * {@code 100 Continue}.
	 * @see <a href="https://tools.ietf.org/html/rfc7231#section-6.2.1">HTTP/1.1: Semantics and Content, section 6.2.1</a>
	 */
	CONTINUE(100, Series.INFORMATIONAL, "Continue"),
	/**
	 * {@code 101 Switching Protocols}.
	 * @see <a href="https://tools.ietf.org/html/rfc7231#section-6.2.2">HTTP/1.1: Semantics and Content, section 6.2.2</a>
	 */
	SWITCHING_PROTOCOLS(101, Series.INFORMATIONAL, "Switching Protocols"),
	/**
	 * {@code 102 Processing}.
	 * @see <a href="https://tools.ietf.org/html/rfc2518#section-10.1">WebDAV</a>
	 */
	PROCESSING(102, Series.INFORMATIONAL, "Processing"),
	/**
	 * {@code 103 Checkpoint}.
	 * @see <a href="https://code.google.com/p/gears/wiki/ResumableHttpRequestsProposal">A proposal for supporting
	 * resumable POST/PUT HTTP requests in HTTP/1.0</a>
	 */
	CHECKPOINT(103, Series.INFORMATIONAL, "Checkpoint"),
..
}



enum 관련 내용 참고
https://onlyfor-me-blog.tistory.com/420

0개의 댓글