@Bean public OpenAPI defineOpenApi() { Serverserver = newServer(); server.setUrl("http://localhost:8080"); server.setDescription("Development");
Contact myContact = new Contact(); myContact.setName("Jane Doe"); myContact.setEmail("your.email@gmail.com");
Info information = newInfo() .title("Employee Management System API") .version("1.0") .description("This API exposes endpoints to manage employees.") .contact(myContact); returnnew OpenAPI().info(information).servers(List.of(server)); } }
如果想要為個別的API添加描述,可以參考以下程式。
1 2 3 4 5 6 7 8 9 10 11 12 13
@Tag(name = "get", description = "GET methods of Employee APIs") @GetMapping("/employees") public List<Employee> findAllEmployees() { return repository.findAll(); }
@Tag(name = "get", description = "GET methods of Employee APIs") @GetMapping("/employees/{employeeId}") public Employee getEmployee(@PathVariable int employeeId) { Employee employee = repository.findById(employeeId) .orElseThrow(() -> new RuntimeException("Employee id not found - " + employeeId)); return employee; }
@Tag中的name代表了分類,description代表針對這個分類的描述。
如果要建立Response的文件規範的話,可以參考以下:
1 2 3 4 5 6 7 8 9 10 11 12
@ApiResponses({ @ApiResponse(responseCode = "200", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Employee.class)) }), @ApiResponse(responseCode = "404", description = "Employee not found", content = @Content) }) @DeleteMapping("/employees/{employeeId}") public String deleteEmployee(@PathVariable int employeeId) { Employee employee = repository.findById(employeeId) .orElseThrow(() ->new RuntimeException("Employee id not found - " + employeeId)); repository.delete(employee); return"Deleted employee with id: " + employeeId; }