Home » Swagger » Spring-Rest-Swagger

Spring-Rest-Swagger

If you are new to swagger and do not know where to star.
See this link first and then come back to this page.

Let’s see one example where I am going to create a simple spring boot rest application and will also use swagger.

Step 1 ::
I have used two jars “springfox-swagger2” and “springfox-swagger-ui” with 2.7.0 version.I have added those in my pom.xml.I have also added spring web related dependencies for my spring restful application.The detail pom.xml you can see below.

pom.xml

Step 2 :: I have created a configuration class to use swagger.Use “@EnableSwagger2” to enable swagger.A Docket bean is defined with “SWAGGER_2” DocumentationType.PathSelectors is kept any() to allow all path patterns.RequestHandlerSelectors’ base package is the controller class package name.Controller class will be the class where we will serve rest call mappings.
SwaggerConfig.java

Step 3 :: I have created a model Employee class with id,name and department attributes for RequestBody.
EmployeeModel.java

Step 4 :: I have created the controller class now where the rest call will hit and will generate response.I have used “@Api” and “@ApiOperation” to provide swagger specifications.

Ai1tutorialSwaggerController.java

Step 5 :: Finally a spring boot main class with base package scan for Config and Controller class.
SpringBootRestApp.java

Now you can run your application.
Console OutPut ::

You can now open the URL in your browser http://localhost:8080/swagger-ui.html .For my case the tomcat server port is 8080

Now you can do post request and get response like below.


2 Comments

Leave a comment

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

19 − 9 =