برنامه نویسی
مستندسازی یک API با Java Spring Boot با استفاده از Swagger

مراحل مستندسازی API RESTful در Java Spring Boot با Swagger
- وابستگی های Swagger را اضافه کنید
وابستگی های Swagger را در فایل pom.xml اضافه کنید:
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
- Swagger را پیکربندی کنید
یک کلاس پیکربندی Swagger ایجاد کنید:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example"))
.paths(PathSelectors.any())
.build()
.apiInfo(new ApiInfoBuilder()
.title("My API")
.description("This is a RESTful API in Java Spring Boot using Swagger")
.version("1.0")
.contact(new springfox.documentation.service.Contact("API Support", "", "support@example.com"))
.build());
}
}
- حاشیه نویسی Swagger را برای توصیف نقاط پایانی، پارامترها و پاسخ ها اضافه کنید
نکات عملیاتی:
برای هر نقطه پایانی، یادداشت هایی با جزئیات درخواست HTTP، مسیر و یک خلاصه اولیه اضافه کنید.
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/movies")
@Api(tags = "Movies")
public class MovieController {
@ApiOperation(value = "Get a list of movies", notes = "Retrieves a list of movies")
@GetMapping
public ListMovie> getMovies() {
// code ...
}
@ApiOperation(value = "Get a movie by ID", notes = "Retrieves a movie by its ID")
@GetMapping("/{id}")
public Movie getMovie(
@ApiParam(value = "ID of the movie to be obtained", required = true)
@PathVariable String id) {
// code ...
}
}
تعریف الگوهای پاسخ:
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(description = "Details about the Movie")
public class Movie {
@ApiModelProperty(notes = "The unique ID of the movie")
private String id;
@ApiModelProperty(notes = "The name of the movie")
private String name;
// getters and setters
}
@ApiModel(description = "Details about the ErrorResponse")
public class ErrorResponse {
@ApiModelProperty(notes = "The error code")
private int code;
@ApiModelProperty(notes = "The error message")
private String message;
// getters and setters
}
- ایجاد مستندات Swagger
برنامه Spring Boot را راه اندازی کنید و به رابط کاربری Swagger UI در مرورگر دسترسی پیدا کنید:
http://localhost:8080/swagger-ui.html
- شیوه های مستندسازی خوب
- از زبان توصیفی و مختصر برای کمک به درک API استفاده کنید.
- ترتیب یادداشت ها را به طور منطقی سازماندهی کنید تا از یک جریان واضح و استاندارد پیروی کنید و نگهداری را آسان تر کنید.
- اسناد را هر زمان که تغییراتی در API ایجاد می شود به روز کنید تا آن را ثابت و مفید نگه دارید.
با دنبال کردن این مراحل، می توانید API خود را در Java Spring Boot با استفاده از Swagger به طور موثر و واضح مستند کنید.