top of page
Writer's pictureRohit Modi

How to implement Swagger UI in Spring Boot 2.6.x

Updated: Dec 18, 2022

Swagger Ui is very useful when it comes to share the API information with others. It inform users about the functions, input parameter, output parameter and it also gives information about all kind of responses such as success, error, not found, internal server error etc., which makes a developer to write more robust code while consuming the API.


Before Spring Boot 2.4.x, implementation of swagger ui was quite easy, it requires following dependency and few classes to open swagger ui for api information

dependency :-

classes :-

1. SwaggerConfig class

This class uses @EnableSwagger2 annotation at class level and contains docket() function

2. WebSecurityConfig class

This class uses @EnableWebSecurity annotation at class level and class extends WebSecurityConfigurerAdapter


But from spring boot version 2.4.x onwards, this set up has changed, and it becomes more tedious if we go with current setup, we have to modify above classes, put extra classes and then achieve what we suppose to implement easily.

More change at the code level for this purpose has always discouraged in software development.

I am going to share a trick to achieve this with less changes, it will not break our existing setup and we don't have to modify/add classes.

Add following dependency along with above dependency in build.gradle/pom.xml

and then put @OpenAPIDefinition annotation instead of @EnableSwagger2 on SwaggerConfig class.

that's all, really amazing right, now we are good to go.

start the server and open http://localhost:8080/swagger-ui.html


That's all folks in this article, I will come up with another article shortly.

A Big thank you for checking out my article, this really encourage me to come up with more topics.


Here are some resources you might be interested ------------------------




358 views0 comments

Comments


Spring Boot
bottom of page