ResponseEntity 정리HTTP 응답에 헤더와 상태 코드 포함 → 부가 정보 전달 가능
| 항목 | 설명 |
|---|---|
ResponseEntity | 응답 본문 + 헤더 + 상태 코드 모두 설정 가능 |
HttpHeaders | 응답 헤더 설정용 객체 |
Content-Type | 데이터 형식을 명시 (application/json) |
HttpStatus.OK | HTTP 200 상태 코드 (성공 응답) |
String message = "{\"name\":\"초코에몽\"}";HttpHeaders header = new HttpHeaders();
header.add("Content-Type", "application/json;charset=UTF-8");return new ResponseEntity<String>(message, header, HttpStatus.OK);enctype="multipart/form-data"multipart/form-data로 인코딩하여 전송하며, 텍스트 필드와 파일 필드를 구분하여 전송함.<form method="POST" enctype="multipart/form-data">
<input type="text" name="username">
<input type="file" name="profile_picture">
</form>
username) → multipart reserveprofile_picture) → multipart files@ControllerAdvice@ExceptionHandler: 예외 처리@ModelAttribute: 공통 모델 속성 설정@InitBinder: 바인딩 초기화@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(NullPointerException.class)
public ResponseEntity<String> handleNullPointer(NullPointerException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body("널포인터 예외가 발생했어요!");
}
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleAll(Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("서버에서 에러가 발생했어요!");
}
}
@ControllerAdvice
public class GlobalModelAdvice {
@ModelAttribute("appName")
public String appName() {
return "나의 스프링 앱";
}
}
${appName}을 사용할 수 있게 만들어줌.