μΆμ² : λ΄μΌλ°°μμΊ ν
Http μνμ½λλ Http μλ΅ ν€λ 첫 μ€μ μΆκ°λμ΄ λ°νλλ μμ μ€ νλμ΄λ€.
https://ryanclaire.blogspot.com/2020/10/HTTP-Overview.html
μ£Όμ μλ΅μνμ½λ μ’ λ₯
200λ²λ : μ±κ³΅
400λ²λ : ν΄λΌμ΄μΈνΈ μλ¬, μλͺ»λ μμ²
500λ²λ : μλ² μλ¬, μμ²μ μ ννμ§λ§ μλ² μλ¬λ‘ μ€λ₯ λ°μ
μ€νλ§μμ μ 곡νλ μνμ½λ μ 보 enum ν΄λμ€
public enum HttpStatus {
// 1xx Informational
CONTINUE(100, Series.INFORMATIONAL, "Continue"),
// ...
// 2xx Success
OK(200, Series.SUCCESSFUL, "OK"),
CREATED(201, Series.SUCCESSFUL, "Created"),
// ...
// 3xx Redirection
MULTIPLE_CHOICES(300, Series.REDIRECTION, "Multiple Choices"),
MOVED_PERMANENTLY(301, Series.REDIRECTION, "Moved Permanently"),
FOUND(302, Series.REDIRECTION, "Found"),
// ...
// --- 4xx Client Error ---
BAD_REQUEST(400, Series.CLIENT_ERROR, "Bad Request"),
UNAUTHORIZED(401, Series.CLIENT_ERROR, "Unauthorized"),
PAYMENT_REQUIRED(402, Series.CLIENT_ERROR, "Payment Required"),
FORBIDDEN(403, Series.CLIENT_ERROR, "Forbidden"),
// ...
// --- 5xx Server Error ---
INTERNAL_SERVER_ERROR(500, Series.SERVER_ERROR, "Internal Server Error"),
NOT_IMPLEMENTED(501, Series.SERVER_ERROR, "Not Implemented"),
BAD_GATEWAY(502, Series.SERVER_ERROR, "Bad Gateway"),
// ...
ν€λ
1. μλ΅ λ³Έλ¬Έμ΄ HtmlμΈ κ²½μ°
Content type: text/html
2. μλ΅ λ³Έλ¬Έμ΄ JsonμΈ κ²½μ°
Content type: application/json
λ³Έλ¬Έ
1. Html
<!DOCTYPE html>
<html>
<head><title>By @ResponseBody</title></head>
<body>Hello, Spring μ μ μΉ νμ΄μ§!!</body>
</html>
2. Json
{
"name":"νκΈΈλ",
"age": 20
}
μμΈμ²λ¦¬λ₯Ό μ λλ‘ νμ§ λͺ»νλ€λ©΄, μ¬μ©μμ μλͺ»λ μμ²μΌλ‘ μΈν μμΈμλ 400 μλ¬κ° μλ 500 μλ¬κ° λ°μν μ μλ€.
μ¬μ©λλ κΈ°λ₯ : Exception Handler
-> μ€νλ§ μμΈμ²λ¦¬ λ°©λ²
1) Controller μ½λ μμ
ResponseEntity ν΄λμ€ μ¬μ© : Http response objectλ₯Ό μν Wrapper ν΄λμ€ ~ Http statusCode, Http headers, Http bodyλ₯Ό λ΄μ μλ΅νλ ν΄λμ€
@PostMapping("/folders")
public ResponseEntity addFolders(
@RequestBody FolderRequestDto folderRequestDto,
@AuthenticationPrincipal UserDetailsImpl userDetails
) {
try {
List<String> folderNames = folderRequestDto.getFolderNames();
User user = userDetails.getUser();
List<Folder> folders = folderService.addFolders(folderNames, user.getUsername());
return new ResponseEntity(folders, HttpStatus.OK);
} catch (IllegalArgumentException ex) {
RestApiException restApiException = new RestApiException();
restApiException.setHttpStatus(HttpStatus.BAD_REQUEST);
restApiException.setErrorMessage(ex.getMessage());
return new ResponseEntity(
// Http Body
restApiException,
// Http StatusCode
HttpStatus.BAD_REQUEST);
}
}
Try - catch λ¬Έμμ μμ²μ΄ μ±κ³΅νμ κ²½μ°μ κ·Έλ μ§ λͺ»ν κ²½μ°λ₯Ό λλκ³ κ°κ° κ·Έ κ°μ ResponseEntity κ°μ²΄λ‘ λ°ννλ€.
2) @ExceptionHandler μ¬μ©
-> FolderController ν΄λμ€ μμ λͺ¨λ λ©μλμ μμΈμ²λ¦¬λ₯Ό μ μ©νλ λ°©λ²(AOP)
@ExceptionHandler({ IllegalArgumentException.class })
public ResponseEntity handleException(IllegalArgumentException ex) {
RestApiException restApiException = new RestApiException();
restApiException.setHttpStatus(HttpStatus.BAD_REQUEST);
restApiException.setErrorMessage(ex.getMessage());
return new ResponseEntity(
// HTTP body
restApiException,
// HTTP status code
HttpStatus.BAD_REQUEST
);
}
1) 2) λͺ¨λ μΌμΌν μΆκ°ν΄μΌνκΈ° λλ¬Έμ λ²κ±°λ‘μμ΄ ν¬λ€.
-> λ°©λ²μ λ€λ₯΄μ§λ§, μμΈλ₯Ό μ²λ¦¬νλ λ Όλ¦¬λ κ°λ€ : μμΈκ° μμλλ λΆλΆμμ μμΈλ₯Ό λ§λ€μ΄ λμ§κ³ , κ·Έ μμΈλ΄μ©μ λ΄μ
ν΄λΌμ΄μΈνΈμ 보λ΄κΈ°
~ κΈλ‘λ² μμΈ μ²λ¦¬
@ControllerAdvice μ¬μ© : μ€κ°μμ μμ²μ κΈ°λ€λ¦¬λ€κ° μμΈκ° λ°μν κ²½μ° μ€κ°μμ μ‘μμ± ν μμΈμ²λ¦¬
@RestControllerAdvice
-> @ControllerAdvice + @ResponseBody
@RestControllerAdvice μ μ©
ν΅μ¬ : κ΄μ¬μ¬λ₯Ό λΆλ¦¬νμ
'Springπ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
λ°μ΄ν° λ² μ΄μ€λ₯Ό λ μμ νκ² κ΄λ¦¬νκΈ°(Primary, Replica) (0) | 2022.12.29 |
---|---|
Spring Transaction (0) | 2022.12.29 |
Spring AOP (0) | 2022.12.28 |
ν μ€νΈ μ½λ (0) | 2022.12.27 |
OAuth2 (0) | 2022.12.27 |