SpringDoc 官方文档
Springdoc3取代swagger2
- pom xml加载Springdoc Jar
- @OpenAPIDefinition,声明一个OpenAPI
- 对API进行分组,方便查询
- 访问地址
- springdoc Configuration
- @RequestMapping
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)