본문 바로가기
Spring

[Spring Framework] ResponseEntity

by Bhinney 2022. 8. 25.

✅ HttpEntity란

  • Spring Framework에서 제공하는 클래스 중 하나이다.
  • HttpEntity는 HTTP요청 또는 응답에 해당하는 HttpHeader와 HttpBody를 포함하는 클래스이다.
// Represents an HTTP request or response entity, consisting of headers and body.

public class HttpEntity<T> {

	/*
	 * The empty {@code HttpEntity}, with no body or headers.
	 */
	public static final HttpEntity<?> EMPTY = new HttpEntity<>();


	private final HttpHeaders headers;

	@Nullable
	private final T body;}

✅ ResponseEntity

  • HttpEntity 클래스를 상속 받아 구현한 클래스이다.
  • ResponseEntity는 사용자의 HttpRequest에 대한 응답 데이터를 포함하는 클래스이다.
    • 따라서 HttpStatus, HttpHeader, HttpBody를 포함한다.
/*
* Extension of HttpEntity that adds an HttpStatus status code. 
* This can also be used in Spring MVC as the return value from an @Controller method.
*/

public class ResponseEntity<T> extends HttpEntity<T>

 

	/**
	 * Create a {@code ResponseEntity} with a status code only.
	 * @param status the status code
	 */
	public ResponseEntity(HttpStatus status) {
		this(null, null, status);
	}

	/**
	 * Create a {@code ResponseEntity} with a body and status code.
	 * @param body the entity body
	 * @param status the status code
	 */
	public ResponseEntity(@Nullable T body, HttpStatus status) {
		this(body, null, status);
	}

	/**
	 * Create a {@code ResponseEntity} with headers and a status code.
	 * @param headers the entity headers
	 * @param status the status code
	 */
	public ResponseEntity(MultiValueMap<String, String> headers, HttpStatus status) {
		this(null, headers, status);
	}

	/**
	 * Create a {@code ResponseEntity} with a body, headers, and a status code.
	 * @param body the entity body
	 * @param headers the entity headers
	 * @param status the status code
	 */
	public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, HttpStatus status) {
		this(body, headers, (Object) status);
	}

	/**
	 * Create a {@code ResponseEntity} with a body, headers, and a raw status code.
	 * @param body the entity body
	 * @param headers the entity headers
	 * @param rawStatus the status code value
	 * @since 5.3.2
	 */
	public ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, int rawStatus) {
		this(body, headers, (Object) rawStatus);
	}

	/**
	 * Private constructor.
	 */
	private ResponseEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers, Object status) {
		super(body, headers);
		Assert.notNull(status, "HttpStatus must not be null");
		this.status = status;
	}

 

'Spring' 카테고리의 다른 글

[Spring] 예외 처리 - Controller에서 처리  (0) 2022.10.27
[Spring] MapStruct 사용하여 Mapper 구현하기  (0) 2022.10.20
[Spring] Spring MVC Controller 어노테이션  (0) 2022.08.24
[Spring] Spring MVC  (0) 2022.08.24
[Spring] AOP란  (0) 2022.08.17

댓글