探 SpringDoc OpenAPI 常用註解

@OpenAPIDefinition 和 Info 這些註解聲明 API 資訊:標題、版本、許可證、安全性、伺服器、標籤、安全性和 externalDocs
java 复制代码
@OpenAPIDefinition(
    info = @Info(
        title = "Accounts microservice REST API",
        description = "Accounts microservice REST API",
        version = "v1.0",
        contact = @Contact(
            name = "Danny Yu",
            email = "[EMAIL_ADDRESS]"
        ),
        license = @License(
            name = "Apache 2.0",
            url = ""
        )
    ),
    externalDocs = @ExternalDocumentation(
        description = "Accounts microservice REST API documentation",
        url = "https://www.demo.com/swagger-ui.html"
    )
)public class AccountsApplication {
    public static void main(String[] args) {
        SpringApplication.run(AccountsApplication.class, args);
    }
}
@Tag 為一組 API 操作新增標籤,以便在文件中組織和分組
@Operation 描述一個 API 操作,包括摘要和詳細描述
java 复制代码
@Operation(summary = "Create Account REST API", description = "REST API to create an account")
    @Tag(name = "Account Controller", description = "Account Management API")
    @PostMapping("/create")
    public ResponseEntity<ResponseDto> createAccount(@Valid @RequestBody CustomerDto customerDto) {
        iAccountsService.createAccount(customerDto);
        return ResponseEntity
                .status(HttpStatus.CREATED)
                .body(new ResponseDto(AccountsConstants.STATUS_201, AccountsConstants.MESSAGE_201));
    }
@ApiResponse述單一 HTTP 回應狀態碼的詳細資訊
@ApiResponses 描述多個 HTTP 回應狀態碼的詳細資訊
@Content 描述回應內容的類型和格式
java 复制代码
 @Operation(summary = "Create Account REST API", description = "REST API to create an account")
    @ApiResponse(responseCode = "201", description = "HTTP Status Created")
    @PostMapping("/create")
    public ResponseEntity<ResponseDto> createAccount(@Valid @RequestBody CustomerDto customerDto) {
        iAccountsService.createAccount(customerDto);
        return ResponseEntity
                .status(HttpStatus.CREATED)
                .body(new ResponseDto(AccountsConstants.STATUS_201, AccountsConstants.MESSAGE_201));
    }
java 复制代码
@Operation(summary = "Update an account", description = "REST API to update an account")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "HTTP Status OK"),
            @ApiResponse(responseCode = "417", description = "Expectation Failed", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponseDto.class))),
            @ApiResponse(responseCode = "500", description = "Internal Server Error", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponseDto.class)))
    })
    @PutMapping("/update")
    public ResponseEntity<ResponseDto> updateAccountDetails(@Valid @RequestBody CustomerDto customerDto) {
        boolean isUpdated = iAccountsService.updateAccount(customerDto);
        if (isUpdated) {
            return ResponseEntity
                    .status(HttpStatus.OK)
                    .body(new ResponseDto(AccountsConstants.STATUS_200, AccountsConstants.MESSAGE_200));
        }
        return ResponseEntity
                .status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(new ResponseDto(AccountsConstants.STATUS_500, AccountsConstants.MESSAGE_500));
    }
@Schema 描述資料模型的屬性和結構,通常用於模型類別或 API 方法的參數和傳回值
java 复制代码
@Data
@Schema(name = "Customer", description = "Schema to hold Customer and Account information")
public class CustomerDto {

    @Schema(description = "Name of the customer", example = "Danny Yu")
    @NotEmpty(message = "Name is required")
    @Size(min = 5, max = 30, message = "Name must be between 5 and 30 characters")
    private String name;

    @Schema(description = "Email of the customer", example = "demo@email.com")
    @NotEmpty(message = "Email is required")
    @Email(message = "Email is invalid")
    private String email;

    @Schema(description = "Mobile number of the customer", example = "1234567890")
    @Pattern(regexp = "\\d{10}", message = "Mobile number must be 10 digits")
    private String mobileNumber;

    @Schema(description = "Account details of the customer", example = "{\"accountNumber\": 1234567890, \"accountType\": \"Savings\", \"branchAddress\": \"123 Main St\"}")
    private AccountsDto accountsDto;
}
相关推荐
1candobetter2 小时前
JAVA后端开发——多模块 Maven 项目 POM 管理规范实践
java·开发语言·maven
敲敲千反田2 小时前
CMS和G1
java·开发语言·jvm
花千树-0102 小时前
MCP HTTP 传输详解:比 SSE 简单,但有一个意外的坑
java·agent·sse·function call·ai agent·mcp·harness
花千树-0102 小时前
三个 Agent 并行调研:用 concurrent 节点构建并发-汇聚式旅游规划助手
java·langchain·agent·function call·multi agent·mcp·harness
2501_913061342 小时前
网络原理之HTTP
java·网络·面试
yaaakaaang2 小时前
二十、状态模式
java·状态模式
一只大袋鼠2 小时前
MyBatis 进阶实战(四): 连接池、动态 SQL、多表关联(一对多 / 多对一 / 多对多)
java·开发语言·数据库·sql·mysql·mybatis
电商API&Tina2 小时前
【1688API接口】1688 开放平台 API 接入心得
java·开发语言·数据库·python·sql·json
Rabitebla2 小时前
【C++】手撕日期类——运算符重载完全指南(含易错点+底层逻辑分析)
java·c语言·开发语言·数据结构·c++·算法·链表