1. 引入依赖
在pom.xml里面引入如下俩个依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.2</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
2. 配置application.yml文件(其实不配置也行)
server:
port: 8080
# 配置swagger文档的访问路径
springdoc:
swagger-ui:
path: /swagger-ui.html
3. 编写swagger配置类SwaggerConfig
package com.config;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SwaggerConfig {
@Bean
public OpenAPI springShopOpenAPI() {
return new OpenAPI()
.info(new Info().title("标题")
.description("我的API文档")
.version("v1")
.license(new License().name("Apache 2.0").url("http://springdoc.org")))
.externalDocs(new ExternalDocumentation()
.description("外部文档")
.url("https://springshop.wiki.github.org/docs"));
}
}
4. 编写一个测试用的controller类
package com.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Tag(name = "测试Controller", description = "这是描述")
public class IndexController {
@GetMapping("/hello")
@Operation(summary = "测试接口")
public String index(@Parameter(name = "name", description = "名称") String name) {
return "hello " + name;
}
}
5. 编写启动类Application
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
然后启动项目,打开浏览器访问 http://localhost:8080/swagger-ui.html
就可以看到生成的文档了
最后附上swagger2到open api的注解变化
文章来源地址https://www.toymoban.com/news/detail-544510.html文章来源:https://www.toymoban.com/news/detail-544510.html
到了这里,关于springboot3使用swagger文档的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!