-
Notifications
You must be signed in to change notification settings - Fork 0
π μ€μ¨κ±° κ°μ΄λλΌμΈ π
-
@ResponseCodeAnnotation
λ₯Ό μ¬μ©νλ€. -
@ResponseCodeAnnotation(CommonResponseCode.μ½λμ΄λ¦)
λ₯Ό 컨νΈλ‘€λ¬μ μ΄λ Έν μ΄μ μΌλ‘ λ£μΌλ©΄ λλ€. -
@ResponseCodeAnnotation μ΄λ Έν μ΄μ μ μ¬μ©νμ§ μλλ€λ©΄ μλμΌλ‘ 200μΌλ‘ μ²λ¦¬λλ€.
μ μ¬μ§κ³Ό κ°μ΄
CREATED
λCREATED
λΌκ³ μ΄λ Έν μ΄μ μ μ¬μ©ν΄μ Swaggerμ μλ €μ£Όλ λͺ¨μ΅.
-
@ExceptionCodeAnnotations
λ₯Ό μ¬μ©νλ€. -
@ExceptionCodeAnnotations({CommonExceptionCode.μ½λμ΄λ¦, CommonExceptionCode.μ½λμ΄λ¦})
-
μλ΅ λ°©μκ³Ό λ€λ₯΄κ²
{}
λ₯Ό μ¬μ©ν΄μ λ°°μ΄ ννλ‘ λ겨μ€λ€. μ΄μ λ μ¬λ¬ μμΈκ° λ°μν μ μλ€κ³ μλ €μ€μΌ νκΈ° λλ¬Έ. -
μμΈκ° 1κ°μ¬λ {}μμ λ£μ΄μΌ νλ€.
μ μ¬μ§μ
BAD_GATEWAY
,BAD_REQUEST
λ₯Ό λ°ννλ λͺ¨μ΅. μ΄λ Έν μ΄μ μ μ¬μ©νμ§ μλλ€λ©΄ μ€μ¨κ±°μλ μμΈκ° μ νμ§ μλλ€.
-
@ApiResponses, @ApiResponse λ₯Ό μ¬μ© ν μλ μμΌλ μλμΌλ‘ μλ΅ νμ, μμΈ νμμ λ§μΆ°μ£Όμ§ μμ μ¬μ©νμ§ μλ κ² μ’λ€. μλ μ¬μ©νμ§ λ§μ
-
μ’ λ μ¬μ©μ νΈλ¦¬νκ², ꡬμλμ§ μκ³ μ¬μ©ν μ μλλ‘ λ§λ€λΌκ³ νμΌλ μ€λ ₯ λΆμ‘±μΌλ‘ μΈν΄ μ€νλ € μ μ½μ΄ μ겨λ²λ¦° λͺ¨μ.
- κ²°λ‘ : λ΄μ£ΌμΈμ.
- Springdoc-openapiλ₯Ό μ¬μ©ν OpenAPI 3.0 μ€νμ λ°νμΌλ‘ κΈμ μμ±νλ€.
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
@Operation(
summary = "User login",
description = "μ¬μ©μκ° λ‘κ·ΈμΈν λ μ¬μ©νλ μλν¬μΈνΈ"
)
@PostMapping(value = "/user3/{id}")
public UserSwagger postUser3(
@PathVariable Long id,
@RequestBody(description = "λ‘κ·ΈμΈμ νμν μ¬μ©μ μ 보", required = true, content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = UserSwagger.class)
)) UserSwagger user) {
return user;
}
-
@Operation
μ API λ¬Έμμ μ€λͺ μ μ μ μ μλ€.-
summary
: μλν¬μΈνΈ κ°λ¨ μ€λͺ -
description
: μλν¬μΈνΈ μμΈ μ€λͺ -
@Content
: λ©ν λ°μ΄ν° μ μ λ° schema μ€μ -
mediaType
: λ©ν λ°μ΄ν° μ€μ μλ΅ κ°λ₯ -
schema
: νμν κ°μ²΄ μ§μ
-
@Schema(description = "μ€μ¨κ±° μ€λͺ
μ© μ¬μ©μ")
@Getter
class UserSwagger {
Integer id = 2759;
@Schema(description = "μ¬μ©μ μ΄λ¦", required = true, example = "μ μκ³ μΆμ΄μ")
String name = "gnoDgnoD";
@Schema(description = "μ¬μ©μ μ΄λ©μΌ", required = true, example = "user@example.com")
String email = "Aleph@tistory.com";
}
-
@Schema
λ₯Ό ν΅ν΄μ κ°μ²΄μ νλκ° μ€λͺ μ μΆκ°ν μ μλ€.-
description
: schemaμμ μ€λͺ ν νλμ κ° -
example
: example Valueμ λ€μ΄κ° μμ κ°
-
@GetMapping("/user/{id}")
public String getPathVariable(
@Parameter(description = "μ¬μ©μ id", required = true)
@PathVariable String id) {
return id;
}
-
Request :
GET /user/123
@GetMapping("/user")
public String getRequestParam(
@Parameter(description = "쿼리λ₯Ό μ
λ ₯ν΄μ£ΌμΈμ")
@RequestParam String name) {
return name;
}
-
Request :
GET /user?name=Name
@RestController
@RequestMapping("/api")
public class UserController {
@PostMapping("/user")
@Operation(
summary = "μ¬μ©μ λ‘κ·ΈμΈ",
description = "μ¬μ©μ λ‘κ·ΈμΈ APIμ
λλ€."
)
public Item createItem(
@RequestBody(description = "μμ±ν μμ΄ν
μ μ 보", required = true, content = @Content(
schema = @Schema(implementation = Item.class)
)) Item item) {
return item;
}
}
-
Request :
POST /user
μ μλ RequestBody κ°
@GetMapping("/user")
public String getRequestHeader(
@Parameter(description = "Authorization token")
@RequestHeader("Authorization") String authorization) {
return authorization;
}
-
Request :
GET /request-header
μ ν€λμ λ΄κ²¨μλAuthorization: Bearer token
@GetMapping("/user")
public String getCookieValue(
@Parameter(description = "Session idκ°μ κ°μ Έμ΄")
@CookieValue("sessionId") String sessionId) {
return sessionId;
}
-
Request :
GET /user
μ μΏ ν€μ λ΄κ²¨μλCookie sessionId=abc123
@PostMapping("/user")
public String createWithModelAttribute(
@Parameter(description = "μμ±ν μμ΄ν
μ μ 보", required = true, schema = @Schema(implementation = Item.class))
@ModelAttribute Item item) {
return "Created item: " + item;
}
-
Request :
POST /user
μ μλ Model κ°
@PostMapping("/user")
public String uploadFile(
@Parameter(description = "μ
λ‘λν νμΌ")
@RequestPart("file") MultipartFile file) {
return file.getOriginalFilename();
}
@RestController
@RequestMapping("/api")
@Tag(name = "User Management", description = "μ¬μ©μ κ΄λ¦¬μ κ΄λ ¨λ API")
public class UserController
- Operationκ³Ό κ°κ°μ νλΌλ©ν° λ³ λ¬Έμ μμ±λ²μ λλ€ μ¬μ©νλ©΄ λλ€!
- π Home
- π λΈλμΉ μ»¨λ²€μ
- β¨ μ»€λ° μ»¨λ²€μ
- π μ΄μ κ°μ΄λλΌμΈ
- π μ€μ¨κ±° κ°μ΄λλΌμΈ
- βοΈ ν μ€νΈ μ½λ κ°μ΄λλΌμΈ
- π¨ κ³΅ν΅ μλ΅ λ° μμΈμ²λ¦¬
- π οΈ μ½λ리ν©ν λ§
- π GitHub Repository
- π Issues
- π Pull Requests
- π½οΈ Project
- ποΈ Wiki