SpringBoot3 integrate SpringDoc

文章介绍了如何使用SpringDoc替换Swagger2来管理和文档化API。首先,通过在pom.xml中引入springdoc-openapi-starter-webmvc-ui依赖,然后利用@OpenAPIDefinition注解声明OpenAPI。接着,通过@GroupedOpenApi将API分组,如restApi和helloApi,并定义了访问路径。最后,配置Springdoc的设置,允许通过/sdk访问文档,并提供了默认的访问URL。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringDoc 官方文档

pom xml加载Springdoc Jar

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

因为是spring-boot-starter-web项目,所以使用springdoc-openapi-starter-webmvc-ui。
需要根据项目类型选择对应的Dependency,否则表现为404 错误。

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

@OpenAPIDefinition,声明一个OpenAPI

@OpenAPIDefinition
@Configuration
public class OpenApiConfig {
	  @Bean
	  public OpenAPI springOpenApi() {
	      return new OpenAPI()
	              .info(new Info().title("This is SpringTest API")
	              .description("Regarding this API to develop mobile application")
	              .version("v1"));
	  }
}

对API进行分组,方便查询

	  @Bean
	  public GroupedOpenApi orderApi() {
		  return GroupedOpenApi.builder().group("restApi").pathsToMatch("/rest/**").build();
	  }
	  @Bean
	  public GroupedOpenApi helloApi() {
		  return GroupedOpenApi.builder().group("helloApi").pathsToMatch("/hello/**").build();
	  }

访问地址

http://localhost:8081/swagger-ui/index.html
or
http://localhost:8081/v3/api-docs

在这里插入图片描述

springdoc Configuration

通过/sdk访问doc,不过Springdoc会自动到/swagger-ui/index.html

springdoc:
  api-docs:
#Default enabled: true
    enabled: true
  swagger-ui:
#defalut path = /swagger-ui/index.html
    path: /sdk

@RequestMapping

@RequestMapping("/hello"),如果不指定RequestMethod,
通过Get,Post,Put,Delete,Options,Head,Patch都可以访问。
and
@RequestMapping(value="/hello",method=RequestMethod.GET)

在这里插入图片描述

### Spring Boot 3 Microservices Integrate Swagger API Documentation Tool In the context of developing microservices using Spring Boot 3, integrating Swagger serves to provide comprehensive and interactive API documentation. This integration enhances developer productivity by offering clear insights into how APIs function. To integrate Swagger within a Spring Boot 3 application: #### Add Dependencies Firstly, include necessary dependencies in `pom.xml` or `build.gradle`. For Maven projects, add these lines inside `<dependencies>` tag[^1]: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` For Gradle users, insert this line under `dependencies` block: ```groovy implementation &#39;io.springfox:springfox-boot-starter:3.0.0&#39; ``` #### Configuration Class Setup Create a configuration class named `SwaggerConfig.java`, which sets up basic configurations for Swagger UI access at `/swagger-ui/index.html`. ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } } ``` This setup ensures that all REST controllers located under specified packages are documented automatically through Swagger. #### Accessing Swagger UI After completing above steps, start the Spring Boot application. Navigate browser to http://localhost:8080/swagger-ui/index.html where detailed information about available endpoints can be explored interactively. By leveraging such tools like Swagger alongside platforms supporting Java applications (such as AWS Elastic Beanstalk, Heroku), developers gain powerful means not only for documenting but also testing their services efficiently during development cycles.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值