java springboot后端配置Swagger, 使用 SpringDoc OpenAPI 库

1. Why use “SpringDoc OpenAPI”, Not use “SpringFox” libraries

Both SpringDoc OpenAPI and SpringFox libraries are popular choices for integrating Swagger with Spring Boot applications. However, there are a few key differences between them:
1.Spring Boot 3.x.x Support: SpringDoc OpenAPI provides better support for Spring Boot 3.x versions, while SpringFox has limited support for Spring Boot 3.x. If you’re using Spring Boot 3.x, SpringDoc OpenAPI is the recommended option.
2.Maintenance and Updates: SpringDoc OpenAPI is actively maintained and regularly updated, while SpringFox seems to be less actively maintained in recent times. This means that SpringDoc OpenAPI is more likely to receive bug fixes and new features, making it a more reliable choice in the long run.
3.Configuration: SpringDoc OpenAPI provides auto-configuration and uses annotations for configuration, while SpringFox typically requires a dedicated configuration class. This can make SpringDoc OpenAPI easier to set up and configure.
Considering these factors, if you’re using Spring Boot 3.x.x or looking for a library that is actively maintained, has good community support, and offers easier configuration, SpringDoc OpenAPI would be the better choice for your project.

2. Configuration Dependencies

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.5.0</version>
</dependency>

3. Controller Code

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class MyController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

4. Accessing the Swagger UI

After starting your Spring Boot application, you can visit Swagger UI to view the automatically generated API documentation. By default, Swagger UI can be accessed at the following URL:

http://localhost:8080/swagger-ui.html

5. Result

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值