36 - OpenAPI และ Swagger
เป้าหมายของบท
Section titled “เป้าหมายของบท”บทนี้จะเพิ่ม OpenAPI document และ Swagger UI ให้โปรเจกต์ เพื่อให้ทีมเห็น contract ของ API ตรงกันและทดลองเรียก API ได้จาก browser
หลังจบบทนี้ผู้อ่านควรเข้าใจ:
- OpenAPI คืออะไร
- Swagger UI ช่วยอะไร
- เพิ่ม dependency springdoc อย่างไร
- ใส่ metadata API อย่างไร
- ใส่ JWT bearer security ใน Swagger อย่างไร
ทำไมต้องมี OpenAPI
Section titled “ทำไมต้องมี OpenAPI”ถ้าไม่มี API documentation คนในทีมต้อง:
- อ่าน source code เอง
- ถาม backend ทุกครั้ง
- เดา request/response จาก Postman
- เจอปัญหา frontend กับ backend เข้าใจ contract ไม่ตรงกัน
OpenAPI ช่วยให้ contract ชัดขึ้น เช่น endpoint, method, request body, response schema และ security
เพิ่ม dependency
Section titled “เพิ่ม dependency”สำหรับ Spring Boot 4 ให้ใช้ springdoc-openapi 3.x:
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:3.0.3'ถ้าใช้ Spring Boot 3 ให้ตรวจ version springdoc 2.x ที่เข้ากับโปรเจกต์ของคุณ
หลังรัน application เปิด:
http://localhost:8080/swagger-ui/index.htmlและ OpenAPI JSON:
http://localhost:8080/v3/api-docsตั้งค่า metadata
Section titled “ตั้งค่า metadata”สร้างไฟล์:
src/main/java/com/example/backendapi/config/OpenApiConfig.javapackage com.example.backendapi.config;
import io.swagger.v3.oas.models.Components;import io.swagger.v3.oas.models.OpenAPI;import io.swagger.v3.oas.models.info.Info;import io.swagger.v3.oas.models.security.SecurityRequirement;import io.swagger.v3.oas.models.security.SecurityScheme;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;
@Configurationpublic class OpenApiConfig {
@Bean public OpenAPI secureAdminOpenAPI() { String schemeName = "bearerAuth";
return new OpenAPI() .info(new Info() .title("Backend API") .version("1.0.0") .description("Spring Boot Backend API")) .addSecurityItem(new SecurityRequirement().addList(schemeName)) .components(new Components() .addSecuritySchemes(schemeName, new SecurityScheme() .name(schemeName) .type(SecurityScheme.Type.HTTP) .scheme("bearer") .bearerFormat("JWT"))); }}ใส่ annotation ใน controller
Section titled “ใส่ annotation ใน controller”ตัวอย่าง AuthController:
@Operation(summary = "Register new user")@PostMapping("/register")public ApiResponse<UserResponse> register( @Valid @RequestBody RegisterRequest request) { return ApiResponse.ok("User registered", authService.register(request));}import:
import io.swagger.v3.oas.annotations.Operation;Swagger กับ JWT
Section titled “Swagger กับ JWT”เมื่อเพิ่ม bearer security แล้ว Swagger UI จะมีปุ่ม Authorize
ขั้นตอนทดสอบ:
- เรียก
/api/v1/auth/login - copy token จาก response
- กด Authorize
- ใส่ token แบบไม่ต้องมีคำว่า
Bearer - ทดลองเรียก
/api/v1/auth/meหรือ admin endpoint
ควรเปิด Swagger ใน production ไหม
Section titled “ควรเปิด Swagger ใน production ไหม”ขึ้นกับระบบ
ถ้าเปิดใน production ต้องควบคุมการเข้าถึง เช่น:
- เปิดเฉพาะ internal network
- เปิดเฉพาะ environment non-prod
- protect ด้วย authentication
สำหรับมือใหม่ แนะนำเปิดใน dev/staging ก่อน และค่อยตัดสินใจ production ตาม policy ของทีม
แบบฝึกหัดท้ายบท
Section titled “แบบฝึกหัดท้ายบท”- เพิ่ม springdoc dependency
- เปิด Swagger UI
- เพิ่ม
OpenApiConfig - ใส่
@Operationให้ register/login - ทดลองใส่ JWT ใน Swagger UI แล้วเรียก
/me