Home  >  Article  >  Java  >  How springboot integrates springdoc

How springboot integrates springdoc

百草
百草Original
2024-01-26 17:29:06939browse

Steps for springboot to integrate springdoc: 1. Add dependencies; 2. Configuration file; 3. Configure Swagger3Config class; 4. Create DTO class; 5. Create API interface and annotations; 6. Start the application and access the API Documentation page. Detailed introduction: 1. Add dependencies, add Springdoc dependencies in the pom.xml file of the Spring Boot project; 2. Configuration files, in the Spring Boot project, etc.

How springboot integrates springdoc

The operating system for this tutorial: Windows 10 system, DELL G3 computer.

To integrate Spring Boot and Springdoc, you can follow the steps below:

1. Add dependencies

in your Spring Boot project In the pom.xml file, add the Springdoc dependency. The following is an example:

<dependency>  
    <groupId>org.springdoc</groupId>  
    <artifactId>springdoc-openapi-ui</artifactId>  
    <version>1.5.3</version>  
</dependency>

2. Configuration file

In the application.yml file of your Spring Boot project, add the following configuration:

springdoc:  
  api-docs:  
    enabled: true # 开启文档功能,默认为true,可不配置  
  swagger-ui:  
    path: /api # 访问路径,默认为/swagger-ui.html

3. Configure the Swagger3Config class

Create a Swagger3Config class and implement the OpenAPISchemaConverter interface. This class is used to configure related parameters of Swagger3. The following is an example:

@Configuration  
public class Swagger3Config {  
    @Bean  
    public OpenAPISchemaConverter openAPISchemaConverter() {  
        return new OpenAPISchemaConverter();  
    }  
}

4. Create a DTO class

Create a DTO (Data Transfer Object) class to define the model of API requests and responses. Use Swagger annotations to describe model fields and types. The following is an example:

@ApiModel(description = "用户模型")  
public class UserDTO {  
    @ApiModelProperty(notes = "用户ID", required = true)  
    private Long id;  
    @ApiModelProperty(notes = "用户姓名", required = true)  
    private String name;  
    // 其他字段和注解...  
}

5. Create API interface and annotations

In your service layer or controller layer, create API interface and annotations, use Swagger Annotations are used to describe API paths, request methods, parameters and other information. The following is an example:

@RestController  
@RequestMapping("/api")  
public class UserController {  
    @ApiOperation(value = "获取用户信息", notes = "根据ID获取用户信息")  
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")})  
    @GetMapping("/user/{id}")  
    public ResponseEntity<UserDTO> getUserById(@PathVariable Long id) {  
        // 实现获取用户信息的逻辑...  
    }  
}

6. Start the application and access the API documentation page

Start your Spring Boot application and enter the application in the browser access address. You should be able to see the Swagger UI interface, which contains your API documentation and available API interfaces.

The above is the detailed content of How springboot integrates springdoc. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn