π μꡬ μ¬ν
μλ APIμ λν΄ API λ¬Έμλ₯Ό μμ±ν΄λΌ.
OpenApiConfig
@Configuration
public class OpenApiConfig {
@Bean
public OpenAPI openAPI(){
Info info = new Info()
.title("μ΅λͺ
κ²μν API λ¬Έμ")
.version("v0.0.1")
.description("μ΅λͺ
κ²μνμ API λ¬Έμμ
λλ€.");
return new OpenAPI()
.components(new Components())
.info(info);
}
}
κ²°κ³Ό
PostController
@RestController
@RequestMapping("/boards")
@RequiredArgsConstructor
public class PostController {
private final PostService postService;
@Operation(summary = "κ²μκΈ μ‘°ν", description = "ν΄λΉ idμ κ²μκΈ μ‘°ν")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "κ²μκΈ μ‘°ν μ±κ³΅",
content = @Content(schema = @Schema(implementation = PostResponse.class))),
@ApiResponse(responseCode = "404", description = "μ‘΄μ¬νμ§ μλ κ²μκΈ μ‘°ν",
content = @Content(schema = @Schema(implementation = ErrorResult.class)))
})
@GetMapping("/{postId}")
public PostResponse getPost(@Parameter(description = "κ²μκΈμ id", in = ParameterIn.PATH)
@PathVariable Long postId) {
return postService.findPost(postId);
}
@Operation(summary = "κ²μκΈ μμ±", description = "μμ² DTOμ λ΄κΈ΄ λ°μ΄ν°λ‘ κ²μκΈ μμ±")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "κ²μκΈ μμ± μ±κ³΅" ,
content = @Content(schema = @Schema(implementation = PostResponse.class))),
@ApiResponse(responseCode = "400", description = "μλͺ»λ μμ²",
content = @Content(schema = @Schema(hidden = true)))
})
@PostMapping
public ResponseEntity<PostResponse> createPost(@Valid @RequestBody PostRequest postRequest){
PostResponse saveResponse = postService.writePost(postRequest);
return ResponseEntity.ok(saveResponse);
}
@Operation(summary = "κ²μκΈ μμ ", description = "ν΄λΉ idμ κ²μκΈμ μμ² DTOμ λ΄κΈ΄ λ°μ΄ν°λ‘ μμ ")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "κ²μκΈ μμ μ±κ³΅",
content = @Content(schema = @Schema(implementation = PostResponse.class))),
@ApiResponse(responseCode = "404", description = "μ‘΄μ¬νμ§ μλ κ²μκΈ μ‘°ν",
content = @Content(schema = @Schema(implementation = ErrorResult.class)))
})
@PatchMapping("/{postId}")
public PostResponse modifyPost(@Parameter(description = "κ²μκΈμ id", in = ParameterIn.PATH)
@PathVariable Long postId,
@Valid @RequestBody PostRequest postRequest) {
return postService.modifyPost(postId, postRequest);
}
@Operation(summary = "κ²μκΈ μμ ", description = "ν΄λΉ idμ κ²μκΈ μμ ")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "κ²μκΈ μμ μ±κ³΅",
content = @Content(schema = @Schema(implementation = PostResponse.class))),
@ApiResponse(responseCode = "404", description = "μ‘΄μ¬νμ§ μλ κ²μκΈ μ‘°ν",
content = @Content(schema = @Schema(implementation = ErrorResult.class)))
})
@DeleteMapping("/{postId}")
public ResponseEntity deletePost(@Parameter(description = "κ²μκΈμ id", in = ParameterIn.PATH)
@PathVariable Long postId) {
postService.deletePost(postId);
return ResponseEntity.ok("μ±κ³΅μ μΌλ‘ μμ λμμ΅λλ€.");
}
@Operation(summary = "κ²μκΈ κ²μ", description = "κ²μ ν€μλλ₯Ό ν΅ν΄ κ²μκΈ μ λͺ©μ κ²μ")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "κ²μκΈ κ²μ μ±κ³΅",
content = @Content(schema = @Schema(implementation = PostResponse.class))),
@ApiResponse(responseCode = "400", description = "κ²μ ν€μλκ° μ‘°κ±΄μ λ§μ§ μμ",
content = @Content(schema = @Schema(implementation = ErrorResult.class)))
})
@GetMapping
public List<PostResponse> searchPostTitle(@RequestParam("keyword") String keyword) {
return postService.searchPostTitleList(keyword);
}
@Operation(summary = "κ²μκΈ λͺ¨λ μ‘°ν", description = "λͺ¨λ κ²μκΈμ μ‘°ν")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "κ²μκΈ μ‘°ν μ±κ³΅",
content = @Content(schema = @Schema(implementation = PostResponse.class))),
@ApiResponse(responseCode = "400", description = "μλͺ»λ μμ²",
content = @Content(schema = @Schema(hidden = true)))
})
@GetMapping("/list")
public List<PostResponse> findAllPost(){
return postService.findAllPosts();
}
}
@Operation
μ λ
Έν
μ΄μ
μ API μμ
μ λν λ€μν μμ±μ μ§μ ν μ μλ€. summary
λ API μμ
μ λν κ°λ¨ν μμ½μ ν μ μμ΅λλ€. description
λ API μμ
μ λν μμΈν μ€λͺ
μ ν μ μμ΅λλ€.
@ApiResponse
μ λ
Έν
μ΄μ
μ μ¬μ©νμ¬ API μμ
μ μλ΅ νμμ μ μν μ μλ€. responseCode
, description
λ‘ μ½λμ κ·Έ μ½λμ λν μ€λͺ
μ ν μ μλ€. κ·Έλ¦¬κ³ content
λ₯Ό ν΅ν΄ μλ΅ λ΄μ©μ λ΄μ μ μλλ°, @Content
λ₯Ό μ¬μ©ν΄μ schema
λ₯Ό ν΅ν΄ μλ΅ μ€ν€λ§λ₯Ό μ§μ ν μ μλ€.
hidden
μ ν΅ν΄ μλ΅μ λν λ΄μ©μ κ°λ¦΄ μ μλ€.
@Paramater
μ λ
Έν
μ΄μ
μ μ¬μ©νμ¬ λ§€κ°λ³μλ₯Ό μ μν μ μλ€. description
λ₯Ό ν΅ν΄μ 맀κ°λ³μμ λν μ€λͺ
μ ν μ μλ€.
κ²°κ³Ό
PostResponse
@Getter
public class PostResponse {
@Schema(description = "κ²μν id")
private final Long id;
@Schema(description = "κ²μν μ λͺ©")
private final String title;
@Schema(description = "κ²μν λ΄μ©")
private final String content;
@Schema(description = "κ²μν μμ±μΌμ")
private final LocalDateTime regTime;
public PostResponse(Post post) {
this.id = post.getId();
this.title = post.getTitle().getValue();
this.content = post.getContent().getValue();
this.regTime = post.getRegTime();
}
}
@Schema
λ₯Ό ν΅ν΄μ ν΄λΉ μ€ν€λ§μ λν μ€λͺ
μ λ¬ μ μλ€.κ²°κ³Ό
π λμ μκ°
@Paramater
μ λ
Έν
μ΄μ
μ ν΅ν΄μ νλΌλ―Έν°λ₯Ό name
μΌλ‘ μμ νλ κ²μ΄ μ μ©λμ§ μλλ€. @Paramater
μ λ
Έν
μ΄μ
μ μ μ©νκ² λ κ²½μ° Swaggerμμ νλΌλ―Έν°κ° μμ μμ΄μ§λ νμμ΄ λ°μνλ€. μ΄ λΆλΆμ μμ§ ν΄κ²°νμ§ λͺ»νλ€.