Swagger λ₯Ό μ¬μ©νλ©΄, API λ¬Έμλ₯Ό μλμΌλ‘ ꡬμ±ν΄μ£Όλ©° μλν¬μΈνΈλ₯Ό ν μ€νΈ ν μ μλ€λ μ₯μ μ΄ μλ€.
@Tag
, @Operation
, @ApiResponses
μ΄λ
Έν
μ΄μ
μ μ¬μ©νλ©΄ μ€μ¨κ±° λ¬Έμμ λΆκ°μ μΈ μ€λͺ
μ μΆκ°μ μΌλ‘ λ£μ μ μμ΄μ API λ¬Έμλ₯Ό μ΄ν΄νλλ° λμμ μ€ μ μλ€.
@RestController
@RequestMapping("/api/v1/posts")
@RequiredArgsConstructor
public class PostApiController {
private final PostService postService;
@Tag(name = "Post", description = "κ²μκΈ κ΄λ ¨ API")
@Operation(summary = "κ²μκΈ μ‘°ν", description = "π‘μΉ΄ν
κ³ λ¦¬μ μκ΄μμ΄ λͺ¨λ κ²μκΈμ νμ΄μ§λ‘ μ‘°νν©λλ€.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "β SUCCESS", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
examples = {@ExampleObject(value = "{\"message\":\"SUCCESS\",\"result\":{\"content\":[{\"postId\":1,\"title\":\"title\",\"content\":\"content\",\"category\":\"category\",\"userId\":1,\"nickName\":\"nickName\",\"userName\":\"userName\",\"createdDate\":\"2023/04/27 21:14\",\"lastModifiedDate\":\"2023/04/27 21:14\",\"imageUrl\":\"imageUrl\",\"view\":0,\"totalNumOfComments\":0,\"totalNumOfLikes\":0},{\"postId\":2,\"title\":\"title\",\"content\":\"content\",\"category\":\"category\",\"userId\":1,\"nickName\":\"nickName\",\"userName\":\"userName\",\"createdDate\":\"2023/05/13 00:15\",\"lastModifiedDate\":\"2023/05/13 00:15\",\"imageUrl\":\"imageUrl\",\"view\":0,\"totalNumOfComments\":0,\"totalNumOfLikes\":0}],\"pageable\":{\"sort\":{\"empty\":true,\"sorted\":false,\"unsorted\":true},\"offset\":0,\"pageNumber\":0,\"pageSize\":20,\"unpaged\":false,\"paged\":true},\"totalPages\":1,\"totalElements\":2,\"last\":true,\"size\":20,\"number\":0,\"sort\":{\"empty\":true,\"sorted\":false,\"unsorted\":true},\"numberOfElements\":2,\"first\":true,\"empty\":false}}")}, schema = @Schema(implementation = Response.class))),
})
@GetMapping
public ResponseEntity<Response<Page<PostGetResponse>>> getAll(Pageable pageable) {
Page<PostGetResponse> response = postService.getAllPosts(pageable);
return ResponseEntity.ok(Response.success(response));
}
@Tag(name = "Post", description = "κ²μκΈ κ΄λ ¨ API")
@Operation(summary = "κ²μκΈ μμ±", description = "<strong>πJWT νμ</strong><br>π‘κ²μκΈμ μμ±ν©λλ€.<br>π¨κ°μ
λ νμμ΄ μ‘΄μ¬νμ§ μμ μ μλ¬ λ°μ")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "β SUCCESS", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
examples = {@ExampleObject(value = "{\"message\":\"SUCCESS\",\"result\":{\"id\":1,\"nickName\":\"nickName\",\"blog\":\"blog\",\"email\":\"email\"}}")}, schema = @Schema(implementation = Response.class))),
@ApiResponse(responseCode = "404", description = "β ERROR (κ°μ
λ νμμ΄ μ‘΄μ¬νμ§ μμ μ)", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
examples = {@ExampleObject(value = "{\"message\":\"ERROR\",\"result\":\"κ°μ
λ νμμ΄ μλλλ€.\"}")}, schema = @Schema(implementation = Response.class)))
})
@PostMapping
public ResponseEntity<Response<PostResponse>> create(Authentication authentication, @Validated @RequestBody PostCreateRequest requestDto, BindingResult br) {
String userName = authentication.getName();
PostResponse response = postService.createPost(userName, requestDto);
return ResponseEntity.status(CREATED).body(Response.success(response));
}
}
μμ κ°μ΄, Controllerμ νΉμ λ©μλμ λΆκ° μ€λͺ μ μΆκ°ν μ μλ€.
@Tag
μ¬μ©μ,
μμ κ°μ΄ κ°μ Tag name
μ΄ λ¬λ €μλ μλν¬μΈνΈλΌλ¦¬ λ¬Άμ μ μλ€.
@Operation
μ μ΅μ
μΈ summary
μ description
μ ν΅ν΄
μ΄λ°μμΌλ‘ μλν¬μΈνΈμ λν λΆκ° μ€λͺ λ μΆκ°ν΄μ€ μ μλ€.
κ·Έλ¦¬κ³ @ApiResponse
λ₯Ό ν΅ν΄ μλ΅ response μ μν μ½λ μμλ₯Ό 보μ¬μ€ μ μλ€.
λ¨, μ΄λ° μμ μ κ±°μΉλ©΄ API λ¬Έμλ μΉμ ν΄μ§κ³ κ°λ μ±μ΄ μ’μμ§μ§λ§ Controller μ½λκ° λ§€μ° μ§μ λΆν΄μ§λ€..
κ·Έλμ, μ΄ λ¬Έμ λ₯Ό μΈν°νμ΄μ€λ₯Ό μ¬μ©νλ©΄ ν΄κ²°ν μ μλ€!
@RestController
@RequestMapping("/api/v1/posts")
@RequiredArgsConstructor
public class PostApiController {
private final PostService postService;
@Tag(name = "Post", description = "κ²μκΈ κ΄λ ¨ API")
@Operation(summary = "κ²μκΈ μ‘°ν", description = "π‘μΉ΄ν
κ³ λ¦¬μ μκ΄μμ΄ λͺ¨λ κ²μκΈμ νμ΄μ§λ‘ μ‘°νν©λλ€.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "β SUCCESS", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
examples = {@ExampleObject(value = "{\"message\":\"SUCCESS\",\"result\":{\"content\":[{\"postId\":1,\"title\":\"title\",\"content\":\"content\",\"category\":\"category\",\"userId\":1,\"nickName\":\"nickName\",\"userName\":\"userName\",\"createdDate\":\"2023/04/27 21:14\",\"lastModifiedDate\":\"2023/04/27 21:14\",\"imageUrl\":\"imageUrl\",\"view\":0,\"totalNumOfComments\":0,\"totalNumOfLikes\":0},{\"postId\":2,\"title\":\"title\",\"content\":\"content\",\"category\":\"category\",\"userId\":1,\"nickName\":\"nickName\",\"userName\":\"userName\",\"createdDate\":\"2023/05/13 00:15\",\"lastModifiedDate\":\"2023/05/13 00:15\",\"imageUrl\":\"imageUrl\",\"view\":0,\"totalNumOfComments\":0,\"totalNumOfLikes\":0}],\"pageable\":{\"sort\":{\"empty\":true,\"sorted\":false,\"unsorted\":true},\"offset\":0,\"pageNumber\":0,\"pageSize\":20,\"unpaged\":false,\"paged\":true},\"totalPages\":1,\"totalElements\":2,\"last\":true,\"size\":20,\"number\":0,\"sort\":{\"empty\":true,\"sorted\":false,\"unsorted\":true},\"numberOfElements\":2,\"first\":true,\"empty\":false}}")}, schema = @Schema(implementation = Response.class))),
})
@GetMapping
public ResponseEntity<Response<Page<PostGetResponse>>> getAll(Pageable pageable) {
Page<PostGetResponse> response = postService.getAllPosts(pageable);
return ResponseEntity.ok(Response.success(response));
}
}
μμ κ°μ μ½λκ° μλ€κ³ κ°μ ν΄λ³΄κ² λ€.
public interface PostApiSpecification {
@Tag(name = "Post", description = "κ²μκΈ κ΄λ ¨ API")
@Operation(summary = "κ²μκΈ μ‘°ν", description = "π‘μΉ΄ν
κ³ λ¦¬μ μκ΄μμ΄ λͺ¨λ κ²μκΈμ νμ΄μ§λ‘ μ‘°νν©λλ€.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "β SUCCESS", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
examples = {@ExampleObject(value = "{\"message\":\"SUCCESS\",\"result\":{\"content\":[{\"postId\":1,\"title\":\"title\",\"content\":\"content\",\"category\":\"category\",\"userId\":1,\"nickName\":\"nickName\",\"userName\":\"userName\",\"createdDate\":\"2023/04/27 21:14\",\"lastModifiedDate\":\"2023/04/27 21:14\",\"imageUrl\":\"imageUrl\",\"view\":0,\"totalNumOfComments\":0,\"totalNumOfLikes\":0},{\"postId\":2,\"title\":\"title\",\"content\":\"content\",\"category\":\"category\",\"userId\":1,\"nickName\":\"nickName\",\"userName\":\"userName\",\"createdDate\":\"2023/05/13 00:15\",\"lastModifiedDate\":\"2023/05/13 00:15\",\"imageUrl\":\"imageUrl\",\"view\":0,\"totalNumOfComments\":0,\"totalNumOfLikes\":0}],\"pageable\":{\"sort\":{\"empty\":true,\"sorted\":false,\"unsorted\":true},\"offset\":0,\"pageNumber\":0,\"pageSize\":20,\"unpaged\":false,\"paged\":true},\"totalPages\":1,\"totalElements\":2,\"last\":true,\"size\":20,\"number\":0,\"sort\":{\"empty\":true,\"sorted\":false,\"unsorted\":true},\"numberOfElements\":2,\"first\":true,\"empty\":false}}")}, schema = @Schema(implementation = Response.class))),
})
@GetMapping
ResponseEntity<Response<Page<PostGetResponse>>> getAll(Pageable pageable);
}
μμ κ°μ API λͺ μΈλ₯Ό μν μ΄λ Έν μ΄μ μ λΆλ¦¬ν μ μλ μΈν°νμ΄μ€λ₯Ό μ μνλ€.
@RestController
@RequestMapping("/api/v1/posts")
@RequiredArgsConstructor
public class PostApiController implements PostApiSpecification {
private final PostService postService;
@GetMapping
public ResponseEntity<Response<Page<PostGetResponse>>> getAll(Pageable pageable) {
Page<PostGetResponse> response = postService.getAllPosts(pageable);
return ResponseEntity.ok(Response.success(response));
}
}
κ·Έλ¦¬κ³ κΈ°μ‘΄ μ½λμ μ€μ¨κ±° κ΄λ ¨ μ΄λ Έν μ΄μ μ μ§μ°κ³
μ μν PostApiSpecification
μΈν°νμ΄μ€λ₯Ό implements
ν΄μ€λ€.
κ·Έλ¬λ©΄, API κ΄λ ¨ μ΄λ Έν μ΄μ μ€λͺ μ μΈν°νμ΄μ€ ν κ³³μ λͺ°μμ μμ±ν μ μμ΄ κΈ°μ‘΄ μ½λμ λΆλ¦¬μν¬ μ μλ€!
μλ°μ μμκ³Ό λ€νμ± κ·Έλ¦¬κ³ Springμ μλ¦¬λ§ μλ©΄ μ½κ² μ μ©ν μ μλ λ°©λ²μ΄μλλ°,
λ€λ¦κ² λ°©λ²μ΄ λ μ¬λΌμ μ μ©νκ² λμλ€.