Home » Swagger with SpringBoot3 Native

Swagger with SpringBoot3 Native

Last Updated on October 3, 2023 by KnownSense

Objective:
To create an aot executable/ native-image of a java based Microservice in SpringBoot3 and integration with Swagger for testing and consuming our RESTful api’s.

Understanding the Basics:

What is Swagger:

Swagger is one of the most popular tools for developers to document REST APIs. It allows you to describe the structure of your APIs which helps developer build a good API documentation. We can automatically generate client libraries for our API in many languages and explore other possibilities like automated testing.
In other terms, Swagger is a set of open-source tools for writing REST-based APIs. It simplifies the process of writing APIs starkly, specifying the standards and providing the tools required to write robust, secure, performant and scalable APIs.

Swagger also offers a tool called the Swagger Editor which helps us design our APIs based on the OpenAPI specification. Seems like we have a standard.
Few Advantages of Swagger but not limited to are:

It has a friendly user interface which helps both developer and client to get a good structured api layout.
Helps automate api related processes and generates interactive and easily testable documentation.

Implementation

To implement Swagger in SpringBoot3 for native-image development, we need to follow the below steps:
A. Add these in pom.xml
for swagger:

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


for native:

              <plugin>
	              <groupId>org.graalvm.buildtools</groupId>
	              <artifactId>native-maven-plugin</artifactId>
              </plugin>

B. Add the below property details in application.yml file


springdoc:
  enable-native-support: true
  swagger-ui.path: /swagger-ui.html


The above is added so as to enable the support for swagger in the microservice and also to give a url path for the ui to open up.

C. Also we need to actually provide few configuration to the app for our Swagger to run properly in the microservice. For the same we have added a class named OpenApiConfig in the app and below is the code for the same.

@Configuration
public class OpenApiConfig {

	@Bean
	public OpenAPI customOpenAPI() {
		return new OpenAPI()
				.components(new Components())
				.info(new Info().title("DemoService").description(
						"Demo Service API").version("0.0.1"));

	}
}

This is all the work you need to do. Now you can build the application as native and run it.
All done!! Swagger is now working with native SpringBoot3 microservices.

Github Link:
coming soon !!

Authored by codingknownsense.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top