Spring Framework에서 제공하는 클래스 중 HttpEntity라는 클래스가 존재한다.
이것은 HttpHeader와 HttpBody를 포함하는 클래스이다.
public class HttpEntity<T> {
private final HttpHeaders headers;
@Nullable
private final T body;
}
HttpEntity를 상속받아 구현한 클래스가 RequestEntity
, ResponseEntity
이다.
여기서 ResponseEntity
는 요청에 대한 응답 데이터를 포함하는 클래스이기에 HttpStatus
, HttpHeaders
, HttpBody
를 포함한다.
public class RequestEntity<T> extends HttpEntity<T>
public class ResponseEntity<T> extends HttpEntity<T>
public class ResponseEntity<T> extends HttpEntity<T> {
private final Object status;
/**
* 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);
}
생성자들을 보면 body
, headers
, status
세가지를 다루는 객체임을 알 수 있다.
ResponseEntity에 사용할 속성들을 객체로서 관리하자.
@Data
public class Message {
private Status status;
private String message;
private Object data;
public Message(Status status, String message, Object data) {
this.status = status;
this.data = data;
this.message = message;
}
}
public enum Status {
BAD_REQUEST(400, "BAD_REQUEST"),
INTERNAL_SERER_ERROR(500, "INTERNAL_SERVER_ERROR"),
NOT_FOUND(404, "NOT_FOUND"),
OK(200, "OK") // 필요에 따라 커스텀하자
;
int statusCode;
String code;
Status(int statusCode, String code) {
this.statusCode = statusCode;
this.code = code;
}
}
@GetMapping(path="/")
@ResponseBody
public ResponseEntity<Message> getAllUserAuth() {
List<UserAuth> userAuthsReadResult = userAuthService.getAllUserAuthInfo();
Message responseMessage = new Message(StatusEnum.OK, "Get All Boards Success!", userAuthsReadResult);
HttpHeaders headers = getHeader("application","json","UTF-8");
return new ResponseEntity<>(responseMessage, headers, HttpStatus.OK);
}
public static HttpHeaders getHeader(String type, String subtype, String charsetName) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(new MediaType(type, subtype, Charset.forName(charsetName)));
return headers;
}