1. dependency
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>
2. properties
springdoc.api-docs.path=/v3/api-docs
springdoc.swagger-ui.path=/swagger-ui.html
3. Configuration
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Info;
import org.springframework.context.annotation.Configuration;
@Configuration
@OpenAPIDefinition(
info = @Info(
title = "Demo API",
version = "1.0",
description = "API documentation for Demo application"
)
)
public class OpenApiConfig {
}
4. Example
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
@RestController
@RequestMapping("/api")
public class HelloController {
@Operation(
summary = "인사 메시지 반환",
description = "이 API는 간단한 'Hello, World!' 메시지를 반환합니다.",
tags = {"Hello API"}
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "성공적으로 메시지 반환"),
@ApiResponse(responseCode = "500", description = "서버 오류")
})
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
@Operation(
summary = "이름 기반 인사 메시지 반환",
description = "이 API는 이름을 입력하여 메시지를 반환합니다.",
tags = {"HI API"}
)
@PostMapping("/hi")
public String sayHi(@Parameter(description = "사용자의 이름", required = true)
@RequestParam(required = true) String name) {
return name != null ? "안녕, " + name + "!" : "안녕, 나의세계!";
}
}
5. Connection swagger URL
http://localhost/swagger-ui/index.html
|