프로그래밍/SpringBoot
ResponseEntity 사용법과 Http 응답코드 정리
플로어코딩
2024. 3. 28. 05:53

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@RestController
public class MyController {
@GetMapping("/example")
public ResponseEntity<String> handleRequest() {
String responseBody = "Hello, world!";
HttpHeaders headers = new HttpHeaders();
headers.add("Custom-Header", "Value");
return ResponseEntity
.status(HttpStatus.OK)
.headers(headers)
.body(responseBody);
}
}
1xx: Informational - Request received, continuing process
2xx: Success - The action was successfully received, understood, and accepted
3xx: Redirection - Further action must be taken in order to complete the request
4xx: Client Error - The request contains bad syntax or cannot be fulfilled
5xx: Server Error - The server failed to fulfill an apparently valid request
1xx (Informational)
- 100 Continue: 클라이언트가 요청을 계속할 수 있음
- 101 Switching Protocols: 클라이언트의 요청에 따라 프로토콜이 변경
2xx (Success)
- 200 OK: 요청이 성공적으로 처리
- 201 Created: 새로운 리소스가 성공적으로 생성
- 202 Accepted: 요청이 받아들여졌지만 처리되지 않았음
- 204 No Content: 응답에 본문이 없음
- 206 Partial Content: 부분적으로 요청이 성공적으로 처리
3xx (Redirection)
- 300 Multiple Choices: 다중 선택이 가능
- 301 Moved Permanently: 리소스가 영구적으로 이동
- 302 Found: 리소스가 일시적으로 이동
- 304 Not Modified: 클라이언트의 캐시가 최신 상태
4xx (Client Error)
- 400 Bad Request: 잘못된 요청이 서버에 전달
- 401 Unauthorized: 인증되지 않은 요청
- 403 Forbidden: 요청이 서버에서 거부
- 404 Not Found: 요청한 리소스가 서버에서 찾을 수 없음
5xx (Server Error)
- 500 Internal Server Error: 서버에서 처리 중에 오류가 발생
- 502 Bad Gateway: 게이트웨이나 프록시 서버에서 잘못된 응답
- 503 Service Unavailable: 서버가 일시적으로 서비스를 사용할 수 없음
- 504 Gateway Timeout: 게이트웨이가 요청을 처리하는 데 시간이 초과
| 100 | Continue | [RFC9110, Section 15.2.1] |
| 101 | Switching Protocols | [RFC9110, Section 15.2.2] |
| 102 | Processing | [RFC2518] |
| 103 | Early Hints | [RFC8297] |
| 104-199 | Unassigned | |
| 200 | OK | [RFC9110, Section 15.3.1] |
| 201 | Created | [RFC9110, Section 15.3.2] |
| 202 | Accepted | [RFC9110, Section 15.3.3] |
| 203 | Non-Authoritative Information | [RFC9110, Section 15.3.4] |
| 204 | No Content | [RFC9110, Section 15.3.5] |
| 205 | Reset Content | [RFC9110, Section 15.3.6] |
| 206 | Partial Content | [RFC9110, Section 15.3.7] |
| 207 | Multi-Status | [RFC4918] |
| 208 | Already Reported | [RFC5842] |
| 209-225 | Unassigned | |
| 226 | IM Used | [RFC3229] |
| 227-299 | Unassigned | |
| 300 | Multiple Choices | [RFC9110, Section 15.4.1] |
| 301 | Moved Permanently | [RFC9110, Section 15.4.2] |
| 302 | Found | [RFC9110, Section 15.4.3] |
| 303 | See Other | [RFC9110, Section 15.4.4] |
| 304 | Not Modified | [RFC9110, Section 15.4.5] |
| 305 | Use Proxy | [RFC9110, Section 15.4.6] |
| 306 | (Unused) | [RFC9110, Section 15.4.7] |
| 307 | Temporary Redirect | [RFC9110, Section 15.4.8] |
| 308 | Permanent Redirect | [RFC9110, Section 15.4.9] |
| 309-399 | Unassigned | |
| 400 | Bad Request | [RFC9110, Section 15.5.1] |
| 401 | Unauthorized | [RFC9110, Section 15.5.2] |
| 402 | Payment Required | [RFC9110, Section 15.5.3] |
| 403 | Forbidden | [RFC9110, Section 15.5.4] |
| 404 | Not Found | [RFC9110, Section 15.5.5] |
| 405 | Method Not Allowed | [RFC9110, Section 15.5.6] |
| 406 | Not Acceptable | [RFC9110, Section 15.5.7] |
| 407 | Proxy Authentication Required | [RFC9110, Section 15.5.8] |
| 408 | Request Timeout | [RFC9110, Section 15.5.9] |
| 409 | Conflict | [RFC9110, Section 15.5.10] |
| 410 | Gone | [RFC9110, Section 15.5.11] |
| 411 | Length Required | [RFC9110, Section 15.5.12] |
| 412 | Precondition Failed | [RFC9110, Section 15.5.13] |
| 413 | Content Too Large | [RFC9110, Section 15.5.14] |
| 414 | URI Too Long | [RFC9110, Section 15.5.15] |
| 415 | Unsupported Media Type | [RFC9110, Section 15.5.16] |
| 416 | Range Not Satisfiable | [RFC9110, Section 15.5.17] |
| 417 | Expectation Failed | [RFC9110, Section 15.5.18] |
| 418 | (Unused) | [RFC9110, Section 15.5.19] |
| 419-420 | Unassigned | |
| 421 | Misdirected Request | [RFC9110, Section 15.5.20] |
| 422 | Unprocessable Content | [RFC9110, Section 15.5.21] |
| 423 | Locked | [RFC4918] |
| 424 | Failed Dependency | [RFC4918] |
| 425 | Too Early | [RFC8470] |
| 426 | Upgrade Required | [RFC9110, Section 15.5.22] |
| 427 | Unassigned | |
| 428 | Precondition Required | [RFC6585] |
| 429 | Too Many Requests | [RFC6585] |
| 430 | Unassigned | |
| 431 | Request Header Fields Too Large | [RFC6585] |
| 432-450 | Unassigned | |
| 451 | Unavailable For Legal Reasons | [RFC7725] |
| 452-499 | Unassigned | |
| 500 | Internal Server Error | [RFC9110, Section 15.6.1] |
| 501 | Not Implemented | [RFC9110, Section 15.6.2] |
| 502 | Bad Gateway | [RFC9110, Section 15.6.3] |
| 503 | Service Unavailable | [RFC9110, Section 15.6.4] |
| 504 | Gateway Timeout | [RFC9110, Section 15.6.5] |
| 505 | HTTP Version Not Supported | [RFC9110, Section 15.6.6] |
| 506 | Variant Also Negotiates | [RFC2295] |
| 507 | Insufficient Storage | [RFC4918] |
| 508 | Loop Detected | [RFC5842] |
| 509 | Unassigned | |
| 510 | Not Extended (OBSOLETED) | [RFC2774][status-change-http-experiments-to-historic] |
| 511 | Network Authentication Required | [RFC6585] |
| 512-599 | Unassigned |
출처 : https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml