Skip to content

πŸ“œ μŠ€μ›¨κ±° κ°€μ΄λ“œλΌμΈ πŸ“œ

Seok Won Park edited this page Aug 18, 2024 · 16 revisions

응닡 및 μ˜ˆμ™Έ 처리 방식

1. 응닡(200) μž‘μ„± 방식

  • @ResponseCodeAnnotationλ₯Ό μ‚¬μš©ν•œλ‹€.

  • @ResponseCodeAnnotation(CommonResponseCode.μ½”λ“œμ΄λ¦„)λ₯Ό μ»¨νŠΈλ‘€λŸ¬μ— μ–΄λ…Έν…Œμ΄μ…˜μœΌλ‘œ λ„£μœΌλ©΄ λœλ‹€.

  • @ResponseCodeAnnotation μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄ μžλ™μœΌλ‘œ 200으둜 μ²˜λ¦¬λœλ‹€.

    Response Code Annotation Example

    μœ„ 사진과 같이 CREATEDλŠ” CREATED라고 μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•΄μ„œ Swagger에 μ•Œλ €μ£ΌλŠ” λͺ¨μŠ΅.


2. μ˜ˆμ™Έ(4XX, 5XX) μž‘μ„± 방식

  • @ExceptionCodeAnnotationsλ₯Ό μ‚¬μš©ν•œλ‹€.

  • @ExceptionCodeAnnotations({CommonExceptionCode.μ½”λ“œμ΄λ¦„, CommonExceptionCode.μ½”λ“œμ΄λ¦„})

  • 응닡 방식과 λ‹€λ₯΄κ²Œ {}λ₯Ό μ‚¬μš©ν•΄μ„œ λ°°μ—΄ ν˜•νƒœλ‘œ λ„˜κ²¨μ€€λ‹€. μ΄μœ λŠ” μ—¬λŸ¬ μ˜ˆμ™Έκ°€ λ°œμƒν•  수 μžˆλ‹€κ³  μ•Œλ €μ€˜μ•Ό ν•˜κΈ° λ•Œλ¬Έ.

  • μ˜ˆμ™Έκ°€ 1κ°œμ—¬λ„ {}μ•ˆμ— λ„£μ–΄μ•Ό ν•œλ‹€.

    Exception Code Annotation Example

    μœ„ 사진은 BAD_GATEWAY, BAD_REQUESTλ₯Ό λ°˜ν™˜ν•˜λŠ” λͺ¨μŠ΅. μ–΄λ…Έν…Œμ΄μ…˜μ„ μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€λ©΄ μŠ€μ›¨κ±°μ—λŠ” μ˜ˆμ™Έκ°€ μ νžˆμ§€ μ•ŠλŠ”λ‹€.


3. μΆ”κ°€

  • @ApiResponses, @ApiResponse λ₯Ό μ‚¬μš© ν•  μˆ˜λŠ” μžˆμœΌλ‚˜ μžλ™μœΌλ‘œ 응닡 ν˜•μ‹, μ˜ˆμ™Έ ν˜•μ‹μ„ λ§žμΆ°μ£Όμ§€ μ•ŠμŒ μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 게 μ’‹λ‹€. μ•„λ‹ˆ μ‚¬μš©ν•˜μ§€ 말자

  • μ’€ 더 μ‚¬μš©μ„ νŽΈλ¦¬ν•˜κ²Œ, κ΅¬μ†λ˜μ§€ μ•Šκ³  μ‚¬μš©ν•  수 μžˆλ„λ‘ λ§Œλ“€λΌκ³  ν–ˆμœΌλ‚˜ μ‹€λ ₯ λΆ€μ‘±μœΌλ‘œ 인해 였히렀 μ œμ•½μ΄ 생겨버린 λͺ¨μˆœ.

    • κ²°λ‘  : λ΄μ£Όμ„Έμš”.

Swagger 버전 및 μ˜μ‘΄μ„±

  • Springdoc-openapiλ₯Ό μ‚¬μš©ν•œ OpenAPI 3.0 μŠ€νŽ™μ„ λ°”νƒ•μœΌλ‘œ 글을 μž‘μ„±ν–ˆλ‹€.

0. Gradle μ˜μ‘΄μ„±

implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'

1. @Operation μ„€μ • 및 λͺ¨λΈ 객체 ν•„λ“œ μ„€λͺ…

@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 : ν•„μš”ν•œ 객체 지정


2. @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에 λ“€μ–΄κ°ˆ μ˜ˆμ‹œ κ°’

      image image


3. 각각의 νŒŒλΌλ―Έν„°λ³„ λ¬Έμ„œ μž‘μ„±ν•˜λŠ” 방법

3-1. @PathVariable

@GetMapping("/user/{id}")
public String getPathVariable(
    @Parameter(description = "μ‚¬μš©μž id", required = true) 
    @PathVariable String id) {
    return id;
}
  • Request : GET /user/123

3-2. @RequestParam

@GetMapping("/user")
public String getRequestParam(
    @Parameter(description = "쿼리λ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”") 
    @RequestParam String name) {
    return name;
}
  • Request : GET /user?name=Name

3-3. @RequestBody

@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 κ°’

3-4. @RequestHeader

@GetMapping("/user")
public String getRequestHeader(
    @Parameter(description = "Authorization token") 
    @RequestHeader("Authorization") String authorization) {
    return authorization;
}
  • Request : GET /request-header의 헀더에 λ‹΄κ²¨μžˆλŠ” Authorization: Bearer token

3-5. @CookieValue

@GetMapping("/user")
public String getCookieValue(
    @Parameter(description = "Session id값을 κ°€μ Έμ˜΄") 
    @CookieValue("sessionId") String sessionId) {
    return sessionId;
}
  • Request : GET /user의 쿠킀에 λ‹΄κ²¨μžˆλŠ” Cookie sessionId=abc123

3-6. @ModelAttribute

@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 κ°’

3-7. @RequestPart

@PostMapping("/user")
public String uploadFile(
    @Parameter(description = "μ—…λ‘œλ“œν•  파일") 
    @RequestPart("file") MultipartFile file) {
    return file.getOriginalFilename();
}

4. RestController μ„€λͺ… μΆ”κ°€ν•˜κΈ°

@Tag

@RestController
@RequestMapping("/api")
@Tag(name = "User Management", description = "μ‚¬μš©μž 관리와 κ΄€λ ¨λœ API")
public class UserController
  • Operationκ³Ό 각각의 νŒŒλΌλ©”ν„° 별 λ¬Έμ„œ μž‘μ„±λ²•μ„ λ‘˜λ‹€ μ‚¬μš©ν•˜λ©΄ λœλ‹€!
Clone this wiki locally