探 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;
}
相关推荐
江江爱编程6 分钟前
java零基础保姆级教学01
java·开发语言
9624567 分钟前
餐饮 SaaS 优惠券系统架构演进(三):优惠计算引擎——商品级计价、冲突策略与优惠分摊
java·数据库·spring boot
vx_Biye_Design9 分钟前
expressDeepSeek社团咨询助手的学生社团管理系统60746-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·python·课程设计·express
vx_Biye_Design24 分钟前
springboot宠物领养与救助平台64334-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·mysql·课程设计·宠物
泡茶喝茶写代码26 分钟前
A股量化数据工程:从 REST 接口到策略信号(第 5 篇):板块成分股映射与对齐
java·python·股票数据api·股票数据·股票数据api接口·股票api数据接口·股票量化数据接口
vx_Biye_Design27 分钟前
django就业信息推荐系统61953-计算机课程设计、毕业设计
java·vue.js·spring boot·python·架构·django·课程设计
知守观43 分钟前
HTML 转 PDF 方案实战:iTextPDF 与 wkhtmltopdf 踩坑复盘,附 Flying Saucer / openhtmltopdf 选型对比
java·后端
2601_962071571 小时前
类变量和全局变量的隔离性有什么区别?
java·开发语言·jvm
海绵宝宝转agent1 小时前
LeetCode100 LRU缓存思路讲解
java·开发语言·缓存
专业程序开发源1 小时前
springboot高校学生社团管理系统74810-计算机课程设计、毕业设计
java·vue.js·spring boot·后端·spring·php·课程设计