Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hardiksinghbehl/multiple-open-api-group-spring-boot
POC showing how to divide endpoint(s) among different Open-API screens
https://github.com/hardiksinghbehl/multiple-open-api-group-spring-boot
java open-api open-api-v3 spring-boot
Last synced: about 1 month ago
JSON representation
POC showing how to divide endpoint(s) among different Open-API screens
- Host: GitHub
- URL: https://github.com/hardiksinghbehl/multiple-open-api-group-spring-boot
- Owner: hardikSinghBehl
- Created: 2021-08-28T04:58:01.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-08-28T06:14:09.000Z (about 3 years ago)
- Last Synced: 2024-10-13T23:41:27.024Z (about 1 month ago)
- Topics: java, open-api, open-api-v3, spring-boot
- Language: Java
- Homepage: https://x2-open-api-groups-spring-boot.herokuapp.com/swagger-ui.html
- Size: 60.5 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Multiple Open-API groups: Spring boot
#### POC showing how to divide endpoint(s) among different Open-API screens
#### [Demo Link (Select definition from top right corner)](https://x2-open-api-groups-spring-boot.herokuapp.com/swagger-ui.html)
---https://user-images.githubusercontent.com/69693621/131207157-e017fc2d-631a-4df5-9063-21d3520df48a.mov
---
## Steps
1. Put the below dependency in pom.xml
```org.springdoc
springdoc-openapi-ui
1.5.10```
2. Put the below mentioned property in the application.properties file
```
springdoc.api-docs.groups.enabled=true
```
3. Divide the controller endpoint(s) of each group in a distinct base package
```
- com.behl.controller.nsfw
- put endpoint(s) that belong in 'nsfw' group here
- com.behl.controller.sfw
- put endpoint(s) that belong in 'sfw' group here
```
4. Define Bean of type GroupedOpenApi for each group and specify the package to scan
```
@Bean
public GroupedOpenApi nsfwOpenApi() {
String packagesToscan[] = { "com.behl.agares.controller.nsfw" };
return GroupedOpenApi.builder().group("nsfw")
.packagesToScan(packagesToscan).build();
}
```
5. (Optional) To customize the Open-API defintion including info, contact, security etc implement OpenApiCustomiser interface and override customize() containing your custom configurations
```
@Configuration
public class NsfwOpenApiCustomizer implements OpenApiCustomiser {@Override
public void customise(final OpenAPI openApi) {
final var info = new Info().title("Not Safe For Work Joke API").version("1.0")
.description("Endpoint(s) to expose to mature audience")
.contact(new Contact().email("[email protected]").name("Hardik Singh Behl")
.url("https://www.linkedin.com/in/hardiksinghbehl/"));
openApi.info(info);
}}
```
Pass the class implementing OpenApiCustomizer in the .addOpenApiCustomiser(OpenApiCustomizer) method in the earlier created GroupedOpenApi Bean
```
@Bean
public GroupedOpenApi sfwOpenApi() {
String packagesToscan[] = { "com.behl.agares.controller.sfw" };
return GroupedOpenApi.builder().group("sfw").addOpenApiCustomiser(nsfwOpenApiCustomizer)
.packagesToScan(packagesToscan).build();
}
```
6. Access the swagger-ui at the below URI containing multiple configured definition/groups
```
http://server:port/swagger-ui.html
```
```
http://localhost:8080/swagger-ui.html
```